# Why is it "bad" to call srand multiple times?

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

## Recommended Posts

I keep reading this, but never an explanation. Being curious, I took a look at Visual Studio's code for rand and srand and all srand does is set a global variable. rand performs a typical pseudo-random number algorithm based on the global variable.

While I don't see any need to call srand multiple times, save for using the same seed to repeat the random values, I cannot see the harm. Why is it "bad"?

##### Share on other sites
What argument are you going to call it with next time? And why? If you want to make your rand sequence more random, throw away random numbers by calling rand() while waiting for something to happen you have no control over (such as a keypress).

##### Share on other sites
<blockquote class="ipsBlockquote" data-author="MarkS" data-cid="5014813"><p>I keep reading this, but never an explanation. Being curious, I took a look at Visual Studio's code for rand and srand and all srand does is set a global variable. rand performs a typical&nbsp;pseudo-random number algorithm based on the global variable.<br />&nbsp;<br />While I don't see any need to call srand multiple times, save for using the same seed to repeat the random values, I cannot see the harm. Why is it "bad"?</p></blockquote><br />because you normally pass a fairly low resolution timer value to srand, so if you do srand , rand, srand rand and both srand calls get passed the same value you will also get the same result from the rand calls. (which makes it very non random)

##### Share on other sites
What argument are you going to call it with next time? And why?

No idea. I'm not asking this because I plan on doing this or am currently doing this. I just want an explanation to go along with the statement.

##### Share on other sites
It's not necessarily calling srand multiple times, it's beginners repeatedly calling srand with time(0) and making a "rand isn't random" thread on gamedev:
for ( int i = 1; i <= 10; i=i+1 )
{
srand( time(0) );
int val = rand()%10 + 1;
cout << val << endl;
}

Edited by fastcall22

##### Share on other sites
Well, you can't call it with the return value from rand(), since it will be the same. And if you already seeded it with a timer, seeding it again soon afterwards is likely to be predictable too. Only call srand with a known value if you need to repeat a random sequence again.

EDIT: Which is incredibly useful e.g. for rerunning simulations or doing a replay playback when you only store keypresses. Edited by Paradigm Shifter

##### Share on other sites
The explanation is that that's not how you should use a pseudo-random number generator. The people that designed the PRNG expect you to seed it once and then extract many numbers from it. If you use it differently, you may get bad results, depending on the exact details of how you use it.

##### Share on other sites

I don't know the context in which you heard that re-using srand is bad, but if you understand that you don't have to re-seed all the time (usually never) then I think you probably understand what's going on and can do what you like.

Edited by Khatharr

##### Share on other sites
I keep reading this, but never an explanation. Being curious, I took a look at Visual Studio's code for rand and srand and all srand does is set a global variable. rand performs a typical pseudo-random number algorithm based on the global variable.

because you just don't need to do that, its like checking variable twice just to make sure its value is consistent. just do

// time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
srand(time()); // well not sure time() is present in MSVC


##### Share on other sites
srand(time()); // well not sure time() is present in MSVC
Yes, time() is standard.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 42
• ### Forum Statistics

• Total Topics
632635
• Total Posts
3007559
• ### Who's Online (See full list)

There are no registered users currently online

×