Random number generation is supported in several ways by the Java SDK (Software Development Kit). In addition, there is some Apache support found in the Commons RNG library. In this recipe, we will demonstrate many of these techniques. We start with the use of the Random class and how it can be used to generate numbers beginning with a specified range. This is followed by an examination of the Math.random method and the ThreadLocalRandom class. In additon, Java 8 provides additional stream techniques for generating random numbers.
We will conclude this section with a demonstration of the Apache Commons RNG: Random Numbers Generators project. This API supports the generation of random numbers with the intent of providing more flexibility, speed, and higher quality results. This may prove to be a better choice for many developers. The rationale for the...