From the docs:
Random.nextInt (n) uses Random.next () less than twice on average - it uses it once, and if the resulting value is higher than the maximum n is less than MAX_INT, it tries again, otherwise the value modulo n is returned (this prevents values ββabove itself high multiple of n below MAX_INT, distorting the distribution), therefore, a return value that is evenly distributed in the range from 0 to n-1.
According to docs java.util.Random.next is implemented as follows
synchronized protected int next(int bits) { seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int)(seed >>> (48 - bits)); }
So the complexity is O (1)
On a side note: -
You can use several tools that can be measured using the micro benchmark. You can find the list here . However, if execution complexity is important to you, you can use Fast Mersenne Twister . (This is an external library for measuring the complexity of execution in the form of Javas random number generators are pretty fast, but statistically bad)
Rahul tripathi
source share