Chapter 87: Random Number Generation: Section 87.1: Pseudo Random Numbers
Chapter 87: Random Number Generation: Section 87.1: Pseudo Random Numbers
import java.util.Random;
...
double randDouble = random.nextDouble(); //This returns a value between 0.0 and 1.0
float randFloat = random.nextFloat(); //Same as nextDouble
NOTE: This class only produces fairly low-quality pseudo-random numbers, and should never be used to generate
random numbers for cryptographic operations or other situations where higher-quality randomness is critical (For
that, you would want to use the SecureRandom class, as noted below). An explanation for the distinction between
"secure" and "insecure" randomness is beyond the scope of this example.
Starting in Java 1.7, you may also use ThreadLocalRandom (source). This class provides a thread-safe PRNG (pseudo-
random number generator). Note that the nextInt method of this class accepts both an upper and lower bound.
import java.util.concurrent.ThreadLocalRandom;
Note that the official documentation states that nextInt(int bound) can do weird things when bound is near 230+1
(emphasis added):
The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to
the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst
In other words, specifying a bound will (slightly) decrease the performance of the nextInt method, and this
performance decrease will become more pronounced as the bound approaches half the max int value.
One can use java.security.SecureRandom in situations where a PRNG with an output that is very hard to predict is
required. Predicting the random numbers created by instances of this class is hard enough to label the class as
cryptographically secure.
import java.security.SecureRandom;
import java.util.Arrays;
Besides being cryptographically secure, SecureRandom has a gigantic period of 2160, compared to Randoms period of
248. It has one drawback of being considerably slower than Random and other linear PRNGs such as Mersenne
Twister and Xorshift, however.
Note that SecureRandom implementation is both platform and provider dependent. The default SecureRandom
(given by SUN provider in sun.security.provider.SecureRandom):
Using the same seed to generate random numbers will return the same numbers every time, so setting a different
seed for every Random instance is a good idea if you don't want to end up with duplicate numbers.
newRandSpot++;
// if we have gone though all the spots then set the value
if (newRandSpot==0){
randomNumbers[q] = t;
}
}
}
}
return randomNumbers;
} else {
// invalid can't have a length larger then the range of possible numbers
}
return null;
}
The method works by looping though an array that has the size of the requested length and finds the remaining
For example if the range is 5 and the length is 3 and we have already chosen the number 2. Then we have 4
remaining numbers so we get a random number between 1 and 4 and we loop through the range(5) skipping over
any numbers that we have already used(2).
Now let's say the next number chosen between 1 & 4 is 3. On the first loop we get 1 which has not yet been taken
so we can remove 1 from 3 making it 2. Now on the second loop we get 2 which has been taken so we do nothing.
We follow this pattern until we get to 4 where once we remove 1 it becomes 0 so we set the new randomNumber to
4.
Apart from int, we can generate random long, double, float and bytes using this class.