Sign in to follow this  
yurian

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

Recommended Posts

yurian    182
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]

Share this post


Link to post
Share on other sites
cNoob    295
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.

Share this post


Link to post
Share on other sites
csscplayer    122
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.

Share this post


Link to post
Share on other sites
Pzc    216
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.

Share this post


Link to post
Share on other sites
yurian    182
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?

Share this post


Link to post
Share on other sites
yurian    182
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! =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this