lundi 12 novembre 2018

C++ randomizer - first random number always a factor of 7

I have a simple C++ program I was using to demo random selection of an array element with my students.

As usual, I seeded using srand (time (0)); before using rand() to generate a number.

The array has seven entries, and the selected entry was the first - subscript 0.

I have now run versions of the program fifteen times over forty minutes and the first randomly-generated number has been a multiple of seven, for example:

1258771276 1258586399 1229409447 1257140997 1256216612 1260855344 1262973026 1266351233

I changed up the program to select five random numbers after seeding, and (after generating an initial multiple of 7), the succeeding numbers behave better.

I understand that "random" means I can't predict the numbers to be different any more than I can predict them to be the same.

I also understand that these are pseudorandom numbers.

I also understand rand() is not expected to be top-grade cryptographic randomization.

Still, this behavior is so odd I am completely flabbergasted. Have I fallen into some odd crack of the C++ runtime? This is using Xcode on MacOS fully patched to Mojave 10.14.1.




Aucun commentaire:

Enregistrer un commentaire