vendredi 28 août 2015

Does C always generate the same random sequence?

I ran a program that called rand() four times. I used the modulus operator to limit the range to 1–6. The integers produced were 2, 5, 4, and 2. I reran the program and got the same numbers. Then I created a brand new program that also called rand() four times, and I still got the integer sequence 2, 5, 4, 2. Then I shut the computer down, powered back up, created another new program that called rand() four times, and still got the sequence 2, 5, 4, 2.

I understand the basics that you need to “seed” the RNG using srand(), which starts the sequence at different points, but I'm just curious: forgetting about seeding for a moment, is the sequence generated by rand() installation, compiler, and/or OS dependent? For example, would any of the following result in a different sequence:

  • uninstalling and reinstalling the C compiler on my computer
  • installing and using a different C compiler on my computer
  • running the program on someone else's computer with the same compiler?
  • running the program on someone else's computer with a different compiler (and perhaps a different OS)?

Or is it just a matter of all C compilers using the same RNG algorithm and so the pseudorandom sequence (starting from the beginning) will be the same for everyone?




Aucun commentaire:

Enregistrer un commentaire