This question is about a comment in this question
Recommended way to initialize srand? The first comment says that srand()
should be called only ONCE in an application. Why is it so?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
The reason is that
srand()
sets the initial state of the random generator, and all the values that generator produces are only "random enough" if you don't touch the state yourself in between.For example you could do:
and then if you call that function repeatedly so that
time()
returns the same values in adjacent calls you just get the same value generated - that's by design.