vendredi 11 septembre 2020

Why is the modulus operator not recommended for defining randrange? [duplicate]

There exists a simple expression for getting a random value from a range:

rand() % (max - min) + min

But some prefer this kind of an expression instead even though it looks more complicated and hard to understand:

min + rand() / (RAND_MAX / (max - min + 1) + 1);

What is the advantage of using the second expression and why not to use the first one?

*The question is also about the second expression




Aucun commentaire:

Enregistrer un commentaire