Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not nearly random

Status
Not open for further replies.

Unrivaled1

IS-IT--Management
Feb 9, 2005
20
US
I've done my homework and found numerous references to the following code being as close to random as VC++ gets:

srand((unsigned)time(NULL));
d100 = 1 + rand()%100;


However, if you run this repeated with no pause you get a steadily increasing "random" value until it crests 100 and starts over again. This is hardly random and I have to believe there must be a more truly random method.

Anyone have any thoughts?
 
You should only call srand once at the beginning of your program, not every time you need a new number.

The srand function seeds the random number generator. Once it is seeded, it can generate the random numbers on its own forever. Whenever you give it the same seed, it will give you the same results, so if you keep calling srand with the current time, then it will only give you a different result after a second of time has elapsed. That is why you just seed it once at the beginning, and then put the second line of code in the loop at you will get much more random numbers.
 
That makes sense. I've tried adding it in near the beginning in several spots, but get a compile error each time. I'm too new to VC++ to know where it should go.
 
Nevermind. I found a spot and its working as promised. Much obliged for the assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top