(solved, thanks so much!) srand() and rand() + questions.

Started by
6 comments, last by yurian 16 years, 9 months ago
Could someone explain to me how this works? I've read 3 or 4 tutorials but I simply cannot understand how to use random numbers. And random numbers are a critical part of an idea i have. I mostly don't understand "seeding". Also, (sorry if I cannot explain this clearly) what If I have many sets of "dice roll from ___ to ___?" Oh one last thing. Why is it a bad idea to use system pause and rest function? [Edited by - yurian on July 8, 2007 9:56:36 PM]
Advertisement
From what I have worked out with seeding and random numbers:

Imagine your computer has a set of random numbers for example:

23 65 71 276 94 1 83 2293 82 73

when you dont seed a random number and start calling the rand function it returns the values in order starting from those imaginary numbers, so if you called rand 3 times you would get:

23, 65 and 71

when you seed a random number, you get a new position in those random numbers. for examle after seeding the start position might be 94. Then if you call rand another 3 times you would get:

94, 1 and 83

This may be incorrect but from what I have read thats what i can gather.
Also by seeding using the system time you can be sure to get random numbers each time your program is run.
Call
srand(time(0));
and then call
rand() % 12 + 1;
Replace 12 with any number you want. Does that help?
Quote:Of course justice isn't equal. Rich people can afford lawyers, where poor people get McLawyers.
... what? I don't understand.
Going with the above, I know that if you replace 12 with 100 you will get a random number between 1-100. If you replace with 1000 you get a random number between 1-1000. The percent sign is the mudulos operator. Look it up to see how to implement it better.

srand(time(0)) just starts the random number generator. Rand gets the number which could be really large. You then have to put the number in the range you want by using modulos.
cNoob has the right idea, just using rand() will get you the same "random" numbers each and every time you run your application. Using srand() you start at another place in the sequence. Although, if you supply the same seed for srand() every time you will get the the same result as with rand() only, with different numbers.
The idea to seed srand() with time() is that you will get a new seed every time you start the application as the time will never be the same (or that's the idea, anyway).
So for your application, call srand() once before you use any rand() and then you can forget about srand() for the rest of the application, normally.
Quote:Original post by csscplayer
Going with the above, I know that if you replace 12 with 100 you will get a random number between 1-100. If you replace with 1000 you get a random number between 1-1000. The percent sign is the mudulos operator. Look it up to see how to implement it better.

srand(time(0)) just starts the random number generator. Rand gets the number which could be really large. You then have to put the number in the range you want by using modulos.


Thanks so much! But whats the time for?

Quote:Original post by Pzc
cNoob has the right idea, just using rand() will get you the same "random" numbers each and every time you run your application. Using srand() you start at another place in the sequence. Although, if you supply the same seed for srand() every time you will get the the same result as with rand() only, with different numbers.
The idea to seed srand() with time() is that you will get a new seed every time you start the application as the time will never be the same (or that's the idea, anyway).
So for your application, call srand() once before you use any rand() and then you can forget about srand() for the rest of the application, normally.


Oh I see thank you very much! =)

This topic is closed to new replies.

Advertisement