If you want a uniformly distributed pseudorandom long in the range of [0,m
), try using the modulo operator and the absolute value method combined with the nextLong()
method as seen below:
Math.abs(rand.nextLong()) % m;
Where rand
is your Random object.
The modulo operator divides two numbers and outputs the remainder of those numbers. For example, 3 % 2
is 1
because the remainder of 3 and 2 is 1.
Since nextLong()
generates a uniformly distributed pseudorandom long in the range of [-(2^48),2^48) (or somewhere in that range), you will need to take the absolute value of it. If you don't, the modulo of the nextLong()
method has a 50% chance of returning a negative value, which is out of the range [0,m
).
What you initially requested was a uniformly distributed pseudorandom long in the range of [0,100). The following code does so:
Math.abs(rand.nextLong()) % 100;
java.util.Random
only uses a 48 bit distribution (see implementation details), so it won't have a normal distribution. – Geoffrey De Smet