samedi 5 décembre 2020

Why does rand() % 100 not generate uniformly distributed random numbers if rand() does? [duplicate]

I‘m reading the C++ reference for rand() which returns a randomly distributed number in a specific range. For getting random numbers in the range 0-99, one can simply use a modulo operation:

int random0to99 = rand() % 100;

In the reference, it is additionally stated that:

Notice though that this modulo operation does not generate uniformly distributed random numbers in the span (since in most cases this operation makes lower numbers slightly more likely).

Why? Why isn‘t rand() % 100 uniformly distributed when rand() is?




Aucun commentaire:

Enregistrer un commentaire