private static long nextLong ( Random rng , long n ) { if ( n <= 0 ) throw new IllegalArgumentException ( "n must be greater than 0" ) ; long bits , val ; do { bits = ( rng . nextLong ( ) << 1 ) >>> 1 ; val = bits % n ; } while ( bits - val + ( n - 1 ) < 0L ) ; return val ; }
Access the next long random number between 0 and n .