3 ms·
An additional argument for the multiply-then-shift instead of using modulo, is that for most random number generators the high bits are more random than in the
by jhncls 7y ago
An additional argument for the multiply-then-shift instead of using modulo, is that for most random number generators the high bits are more random than in the low bits.
Another approach, if you really want to use modulo, is to execute the division as a multiplication by the reciprocal followed by a shift. This makes sense when the divisor is odd and used multiple times. See e.g. [0] about how compilers generate code to divide by a constant.
[0] https://en.wikipedia.org/wiki/Division_algorithm#Division_by_a_constant https://en.wikipedia.org/wiki/Division_algorithm#Division_by...