lundi 2 avril 2018

Why use modulo instead of division for generating a random number within a range

I understand that random number generators use the modulo operator to generate a random number within a range. What I am curious is why is it better to use that than division. For example, I could generate a random number in the range of min to max by using the equation:
(max-min) * random_number/maximum_possible_number + min
where maximum_possible_number is the largest possible number that can be represented.
This works because random_number/maximum_possible_number generates a number between 0 and 1. When that's multiplied by max-min it is a number between max-min.
Why is using a modulo algorithm better than this algorithm?




Aucun commentaire:

Enregistrer un commentaire