3 ms·
(r_dist * n) / <max value of r_dist> is better than r_dist % n that linear scaling should mitigate such bias from modulo bias.
by eisbaw 4y ago
(r_dist * n) / <max value of r_dist>
is better than
r_dist % n
that linear scaling should mitigate such bias from modulo bias.
- yorwba 4y agoThat just moves the bias to other numbers. In the example of reducing a byte-sized random value modulo 107, the bias is that 0 can be generated by three different possible inputs (0, 107 and 214), while 42 can only be generated by two (42 and 149; 256 is just out of range), so 0 ends up being 50% more common than 42 in the long run. With your proposed scheme, 0 can be generated by three possible inputs again (0, 1 and 2), while 1 can only be generated by two (3 and 4), so 0 ends up being 50% more common than 1 in the long run.