Is it allowed for srand(0) to have the same effect as srand(1)?
The text you quote from the language spec does not indicate otherwise, nor do I have any other reason to think otherwise. You emphasize the words "new sequence", but that in no way implies that different seeds must produce different sequences.
In fact, the description you quote in no way conditions the newness of the subsequent PRN sequence on the value of the key presented to srand()
. Consider this function, then:
void rtest(void) {
srand(42);
int x1 = rand();
srand(42);
int x2 = rand();
if (x1 == x2) {
puts("equal");
} else {
puts("unequal");
}
}
I expect execution of that function to print "equal", and more generally, that an arbitrary number of PRNs generated after the first srand()
will be equal to the same number of them generated after the second. In what sense, then, has the second srand()
call fulfilled its obligation to use the specified key for a new sequence of PRNs?
The "new" should be understood in the sense of discontinuing use of the current sequence of pseudorandom numbers, and starting to use the one characterized by the specified seed from its beginning, regardless of which sequence that is. There is no requirement that different keys characterize different sequences, though that will affect perceived quality of implementation.
srand(0)
to have the same effect assrand(1)
? – Affinitivesrand(0)
as a special case. Only that not calling srand is the same assrand(1)
. – Shoppingsrand(42)
andsrand(1337)
could return the same "random" sequence. – Oesophagusint rand(void) { return 4; /* chosen by fair dice roll */ }
is allowed. – Affinitive