mardi 24 novembre 2015

Why does rand() produce the same value when seeded with 1 and UINT_MAX?

Here's some code:

#include <iostream>

int main() {
    srand(1);
    std::cout << rand() << "\n";
    srand(UINT_MAX);
    std::cout << rand() << "\n";
}

This produces the following output:

16807
16807

Why do these two seeds produce the same result? The range of possible values is too large for this to be a pure coincidence. Is it:

  • An accident of the implementation of rand() (and if so, I'm curious what that might be)
  • By design (and if so, why?)

(Possibly related: the seeds 10, 100, 1000, 10000, and 100000 produce 168070, 1680700, 16807000, 168070000, and 1680700000 respectively.)




Aucun commentaire:

Enregistrer un commentaire