Monthly Archives: October 2014

Random number generator using modulo

You have probably heard about modulo bias using random number generator. If you read Effective Java by Joshua Bloch this problem is described in Item 47. But did you ever think why there is a problem with simple code like this Math.abs(rnd.nextInt()) % n ? Why the distribution of elements is not equal and by how much is it exactly biased? Here is the mathematical explanation:

Read the rest of this entry