How often should I call srand() in a C++ application?
Asked Answered
S

4

12

I have a C++ application which calls rand() in various places. Do I need to initialize srand() regularly to ensure that rand() is reasonably random, or is it enough to call it once when the app starts?

Shalne answered 5/8, 2011 at 9:53 Comment(6)
srand does not make rand "more" random. It doesn't ensure "reasonable randomness" in any way. It just makes the sequence of random numbers start from the specified point.Unnatural
@jalf I'm surprised that in over 8 years since this comment was made it has not been challenged. The claim "It just makes the sequence of random numbers start from the specified point" seems to be contradicted by cplusplus.com: "For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand."Hypersonic
@Theod'Or, both jalf comment and your quote from cplusplus say pretty the same thing. What's the difference?Shalne
@Shalne The difference is in starting from a specified point versus generating a different succession of results. The former suggests that the range of numbers generated will always be the same, no matter what the seed, the latter suggests different numbers for different seeds. I guess your reading of the latter is that it may also be interpreted to possibly mean just a different starting point in an otherwise unchanging sequence. But in that case the cplusplus statement is only true until the end of the generation period.Hypersonic
Actually, Wikipedia claims rand() is a linear congruential generator, and as their illustration shows, a new seed, if it does not match a number the generator would produce with the previous seed, will result in a different sequence of numbers. If the seed matches though, only the starting point changes.Hypersonic
Except that I've just done a test and do not get a different starting point with a seed matching a previous output, but a different sequence. I suspect the Wikipedia article is out of date and rand() is no longer a simple linear congruential generator.Hypersonic
S
18

If you have only a single thread, seed once. If you reseed often, you might actually break some of the statistical properties of the random numbers. If you have multiple threads, don't use rand at all, but rather something threadsafe like drand48_r, which lets you maintain a per-thread state (so you can seed once per thread).

Slather answered 5/8, 2011 at 9:56 Comment(5)
Boost::random is more portable than drand48_r, or use the C++0x random (which is a Boost derivative)Climb
@MSalters: Of course, I'd go for the new <random> features any day. They're just a little more involved to set up, so I thought I answer in the spirit of the question -- but indeed, do use <random> if you can!Slather
Thanks for this very complete answer.Shalne
"If you reseed often, you might actually break some of the statistical properties of the random numbers." Some evidence to back up this claim would be very welcome.Hypersonic
@Theod'Or: By omission: RNGs don't generally make guarantees about their initial value relative to the seed, but only to the statistics of a rollout starting at (any) one fixed seed.Slather
L
4

Only once, at the start of your application.

Lees answered 5/8, 2011 at 9:55 Comment(0)
M
4

No just calling once is fine. Use the seed value to make the random sequence the same on each execution. This could be useful in making (for example) a game's behaviour deterministic when you replay it for debugging.

Martian answered 5/8, 2011 at 9:55 Comment(0)
G
3

call it once when the app starts

Grigsby answered 5/8, 2011 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.