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

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
Call
srand(time(0));
and then call
rand() % 12 + 1;
Replace 12 with any number you want. Does that help?

##### Share on other sites
... what? I don't understand.

##### Share on other sites
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 on other sites
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 on other sites
Quote:
 Original post by csscplayerGoing 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 on other sites
Quote:
 Original post by PzccNoob 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 on other sites

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628651
• Total Posts
2984044

• 10
• 9
• 9
• 10
• 21