I need to convert a number in the range [0,255] to the range [0,X], where X is smaller than 255 and bigger than 20. The algorithm has these requirements:
- The algorithm should be repeatable, so a given input number should always result in the same output number.
- The algorithm should generate a uniform distribution of numbers in the output range, no biases.
- The algorithm should be architecture independent. It should avoid biases introduced by potential rounding errors in floating point maths, but different precision on different systems shouldn't give different results either.
Simply using modulo is biased for many values of X. Dividing the input down and rounding the result would introduce rounding errors, and therefore biases. I guess it might also be architecture dependent.
One idea might be to use the input as a seed to a simple random number generator (like the Linear Congruential Generator mentioned at Special simple random number generator) and use that to predictably generate any number of bits required. Doing that I could reduce this issue to something similar to what is done in the accepted answer here, which is to divide the input into an evenly distributed set of boxes:
How to generate a random number from within a range
Is there a better way? Am I overcomplicating things?
Aucun commentaire:
Enregistrer un commentaire