When generating random numbers, people first call srand(int seed)
to generate the seed, then call rand(void)
.
My question is why not directly use one single function as rand(int seed)
.
The C standard has a quote which I don't understand, saying
The implementation shall behave as if no library function calls the srand function.
I'm not sure if it has something to do with this statement.
The standard also gives an example implementation, which I don't understand the reason of using a static variable:
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
Aucun commentaire:
Enregistrer un commentaire