lundi 3 avril 2017

Does C's rand() have to be random?

I was trying to get some info on the specification and implementation of rand() in C, and I can't find much information. As a matter of fact, I can't find anything apart from:

  • rand() does not advance between function calls
  • same seed always results in same numbers
  • the random numbers are between 0 and RAND_MAX

Notably, none of these things require randomness. Specifically, I don't see anything that prohibits this implementation:

int randval = 0;

void srand(unsigned int seed) {
    randval = seed;
    return;
}

int rand() {
    return randval++;
}

This seems somewhat unrandom. Is there a bit of standard I'm missing?

(Also, is is bad to seed rand() with time(), then seed ISAAC with rand()?)




Aucun commentaire:

Enregistrer un commentaire