Public Group

srand() problem

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

Recommended Posts

Many sources encourage the use of srand(time(0)) or srand(time(NULL)). But I noticed something weird:
#include <iostream>
#include <time.h>
using namespace std;

void main()
{
srand(time(NULL));
cout<<rand()<<endl;
}


. If I run this program repeatedly, I noticed that the output values are very similar, and they are steadily increasing- so no randomness! Eg first I get 25196, then 25203, 25435 etc. Why is this?

Share on other sites
Quote:
 Original post by asdqweMany sources encourage the use of srand(time(0)) or srand(time(NULL)).But I noticed something weird: *** Source Snippet Removed ***. If I run this program repeatedly, I noticed that the output values are very similar, and they are steadily increasing- so no randomness! Eg first I get 25196, then 25203, 25435 etc.Why is this?
time() will return the same value if you call it within a second (Since it returns values in seconds). That'd cause it to show the same values if you run it quickly.

As for steadily increasing values, that just sounds like coincidence to me. The first few values might be like that I suppose, but it should give roughly random numbers after that, so long as you only call srand() once at startup (Which you should be doing).

Share on other sites
To Evil Steve: Coincidence? I've just run it 40 times- the numbers are steadily increasing- 28210, 28221, 28240, 28254 etc... And I run the exact code I've provided!

Share on other sites
Quote:
 As for steadily increasing values, that just sounds like coincidence to me. The first few values might be like that I suppose, but it should give roughly random numbers after that, so long as you only call srand() once at startup (Which you should be doing).

I have to say I believed this too. But then I just tried it out:

20192202712031020329203592045720502205742060020626206332063620640

These are the numbers I got on a quick run through. After the first call, however, it's more random. VS.NET 2005. It looks like the first value returned from the random function after seeding is more closely related to the seed than each additional call - as in, since the time is increasing, so are these numbers. I don't know what the internal algorithm is used by the VS.NET C++ rand() function, however, and after the first call to rand the numbers were properly "randomized" so not really a huge issue - just interesting.

Share on other sites
You could always seed with clock() instead:
#include <iostream>#include <time.h>using namespace std;void main(){	srand(clock());	cout<<rand()<<endl;}
That should give you a more "random" first value. But as CaspianB said, it really doesn't matter that much - it's very unlikely you'll call rand() once and only once.

Or try rand_s()

Share on other sites
The rand() function is often implemented with a linear congruence PRNG, i.e. the first value will be (time(null)*A+B)%C, with A,B and C being some constants.

Share on other sites
If you are not satisfied with the first random number, just use the second:

#include <iostream>#include <ctime>using namespace std;int main(){	srand(time(0));	rand(); // discard first random number	cout << rand() << endl;}

Thanks!

Share on other sites
You may want to use a more sophisticated random generator instead, such as a Mersenne twister. Depending on how important this is to you, of course. :)

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 22
• 16
• 9
• 17
• Forum Statistics

• Total Topics
632609
• Total Posts
3007399

×