mercredi 25 mars 2015

Keep uniform distribution after remapping to a new range

Since this is about remapping a uniform distribution to another with a different range, this is not a PHP question specifically although I am using PHP.


I have a cryptographicaly secure random number generator that gives me evenly distributed integers (uniform discrete distribution) between 0 and PHP_MAX_INT.


How do I remap these results to fit into a range in an efficient manner?


Currently I am using $mappedRandomNumber = $randomNumber % ($range + 1) + $min where $range = $max - $min, but that obvioulsy doesn't work since the first PHP_MAX_INT%$range have a higher chance to be picked, breaking the uniformity of the distribution.





Aucun commentaire:

Enregistrer un commentaire