Random numbers...

Started by
18 comments, last by Noods 22 years, 7 months ago
Hi all. Im trying to generate a random number with the built in rand and srand abilities, but I cannot get this to "re-seed." I have coded these to generate a random number between 0 and a specified max, but all that happens is the main seed is generated, and I get the same random (ha!) number through out the program. Any help is appreciated. If someone has an alternate method of generating random numbers they wouldnt mind sharing that would be awesome. Here is the code I am using, it is a simple bit of code that would eventually be elaborated on to make a D&D like character generation program. I have included multiple couts in my loop to identify what value the variables held at different time in the executable. -Noods #include #include #include #include int genestat(void); int main() { int str, dex, ite, wis, con, cha; str = genestat(); dex = genestat(); ite = genestat(); wis = genestat(); con = genestat(); cha = genestat(); cout << "STR = " << str; cout << "\nDEX = " << dex; cout << "\nINT = " << ite; cout << "\nWIS = " << wis; cout << "\nCON = " << con; cout << "\nCHA = " << cha << "\n"; cin >> str; return 0; } int genestat(void) { int diea, dieb, diec, stat; const int RANDMAX = 6; srand(time(NULL)); diea = int(1 + rand() % RANDMAX); cout << diea << "\n"; dieb = int(1 + rand() % RANDMAX); cout << dieb << "\n"; diec = int(1 + rand() % RANDMAX); cout << diec << "\n"; stat = diea + dieb + diec; return stat; } http://members.home.net/robin8/paladinmove.gif
Advertisement
It might be because the ''+'' (addition) operator has a higher precedence than a ''%'' (modulo) operator, I''m not sure though (I''ll have to check on that). Meanwhile, just do this:
  /* On startup (as you seem to know): */srand(time(NULL));/* When ever you want some numbers: */#define GetRandom(min,max) ((rand() % max) + min)int Between1And10 = GetRandom(1,10);  


[Resist Windows XP''s Invasive Production Activation Technology!]
Don''t call srand() more than once. It''s that simple. You''re supposed to call srand() just once, at the start of the program before you call rand(), and then call rand() as many times as you like. (Now, for more advanced applications, you might want to call srand() more often, but in this case, you almost certainly don''t.)

So, call srand() at the start of int main(), and take it out of genestat(), and your program should run perfectly.

The reason your code returns all the same random numbers is because srand() keeps reseeding your random number generator to exactly the same number (time(NULL) takes a whole second to return a new value).
Kylotan has your answer for you. Jsut one more important piece of advice though... for anyone using rand() that comes with most versions of the Standard Library (ie most compilers): if you are concerned about how random your random numbers are, never use rand()!!! It is well known that it SUCKS as a random number generator having a period of only a hundred thousand function calls (approximately). If you want a better generator, either a) do a google search and find anything written by Donald Knuth, or b) send me an email and I''ll forward you some C code for a good generator.

Cheers,

Timkin
Wow guys thanks for your help, I have been looking for the answer for this problem for months now! I guess college professors arent that useful If you do have code that is more effective, please feel free to send it to robin8@home.com

Thanks again!
-Noods

http://members.home.net/robin8/paladinmove.gif
Now lets see if I can debug my HTML better than I can debug my C++...

http://members.home.net/robin8/paladinmove.gif
One more time...

quote:Original post by Timkin
Kylotan has your answer for you. Jsut one more important piece of advice though... for anyone using rand() that comes with most versions of the Standard Library (ie most compilers): if you are concerned about how random your random numbers are, never use rand()!!! It is well known that it SUCKS as a random number generator having a period of only a hundred thousand function calls (approximately). If you want a better generator, either a) do a google search and find anything written by Donald Knuth, or b) send me an email and I''ll forward you some C code for a good generator.

Cheers,

Timkin


Here is a fast little RNG called the "Mersenne Twister" that can be used for commercial purposes:

http://www.math.keio.ac.jp/~matumoto/emt.html

Eric
FYI, here''s the code that most standard libraries use for rand and srand
  static unsigned long int value = 1;int rand(void){value = value * 1103515245 + 12345;return((value >> 16) & 0x7FFF);}void srand(unsigned int seed){value = seed;}  


Not too impressive is it?

Nutts

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I have actually seen the ''MT'' but I am still learning C++ and I dont quite understand the code. I have found that the rand and srand dont generate quality random numbers, I usually ended up with duplicate stats, and stats that were constantly too low. I am optimally looking for a function that I can send a maximum, minimum and possibly an interval to, and it can send back a random number within the givens sepecifications. Im not expecting anyone to code this out and send it to me, (although it would be nice) but any incite on how to do this would be appreciated. The problem is, is that generating a quality random number is not easy, especially for someone who is semi new. Timkin - I have emailed you but have not received a response. No rush, just letting you know. Thanks for the help.

-Noods

This topic is closed to new replies.

Advertisement