C++ and Random Numbers

Started by
6 comments, last by kaiel090x 22 years, 8 months ago
I want to create a script not saying random in general, but like random 4. between 0 and 4 only. I am not sure but im guessing I would use rand(4);, since rand() is the ay to create a random number in general? ~ from the depths of the ocean
"He who fights monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you."~Friedrich Nietzsche
Advertisement

Between 0 and 4 inclusive:

num = rand()%5; 




to make it a little more random try this:



int iVar = 0;

srand( time ( 0 ) );
// need to include time.h for this to work
// this just creates a seed according to the time of day

iVar = ( rand() % 5 );



Then you will have a random integer between 0 and 4.




Edited by - -X-Modena-X- on August 10, 2001 3:30:08 PM
I included the time header file, and the iostream header file, to include cout so I could test the "randomness" but I got an error. This is how I wrote it.
//upon posting, the iostream.h and time.h dont show, because of
//the html backets, so i put them in () instead
//I didnt do that in the script though.lol
#include(iostream.h)
#include(time.h)

int main()
{

int iVar = 0;

srand( time ( 0 ) );

iVar = ( rand() % 2 );
//I added the next line to test it
cout <<"iVar = "<< iVar <<" .\n";
}

but it is not working. I am just trying to make something that set a bool variable to true or flase, allowing a player to hit the enemy if its true(meaning his swing was accurate) and false(meaning his swing was innacurate)(this is my first battle system in C++)(I am a flash programmer)

quote:Original post by -X-Modena-X-
to make it a little more random try this:



int iVar = 0;

srand( time ( 0 ) );
// need to include time.h for this to work
// this just creates a seed according to the time of day

iVar = ( rand() % 5 );



Then you will have a random integer between 0 and 4.




Edited by - -X-Modena-X- on August 10, 2001 3:30:08 PM




~ from the depths of the ocean

Edited by - kaiel090x on August 10, 2001 3:59:48 PM
"He who fights monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you."~Friedrich Nietzsche
  #include <iostream>#include <cstdio>#include <ctime>using namespace std;int main (){    srand (time (0));    int iVar = rand () % 2;    cout << "iVar = " << iVar << ".\n";    return 0;}  

This program compiles & works fine.

A couple of notes:
- the statement "it doesn''t work" is absolutely useless. If it doesn''t compile, say it doesn''t compile & list the compiler error (and the lines that generate it). If it doesn''t link, say it doesn''t link & list the link error. If it compiles & runs but doesn''t give you the behavior you expect, describe the behavior.
- never use iostream.h. It''s nonstandard & bad news.
- if you declare main to return an int, return an int.
- names in the C++ Standard Template Library (STL) are generally in namespace std. This includes the iostream classes and the cout global stream. You''ll have to specify namespace std to use them. The reason yours was compiling was because you were using hte nonstandard header file, which do not use namespace std.
Perhaps you might want to include stdlib.h where rand and srand are declared?
There was another reason it didnt compile(see I said it correctly this time) is because of the compiler I was using. It was Quincy 99, and it was just a badly made compiler. I tested the script you gave me in it and the compiler gave me errors. So I put it in MetroWerks CodeWarrior and it worked fine. Thank you very much for your help.



quote:Original post by Stoffel
    #include <iostream>#include <cstdio>#include <ctime>using namespace std;int main (){    srand (time (0));    int iVar = rand () % 2;    cout << "iVar = " << iVar << ".\n";    return 0;}    

This program compiles & works fine.

A couple of notes:
- the statement "it doesn''t work" is absolutely useless. If it doesn''t compile, say it doesn''t compile & list the compiler error (and the lines that generate it). If it doesn''t link, say it doesn''t link & list the link error. If it compiles & runs but doesn''t give you the behavior you expect, describe the behavior.
- never use iostream.h. It''s nonstandard & bad news.
- if you declare main to return an int, return an int.
- names in the C++ Standard Template Library (STL) are generally in namespace std. This includes the iostream classes and the cout global stream. You''ll have to specify namespace std to use them. The reason yours was compiling was because you were using hte nonstandard header file, which do not use namespace std.



~ from the depths of the ocean
"He who fights monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you."~Friedrich Nietzsche
Look for a post in the General Programming section of the forums on random numbers. I explained there why using the % operator to generate a range of random numbers is bad, very bad.

This topic is closed to new replies.

Advertisement