Java Tutorial on the Random Method
Randomly generated numbers have many uses in computer programs, such as creating unpredictability in games, modeling simulations and performing encryption. Java provides two techniques for generating random numbers. The Math library offers a simple, but convenient, random method for generating a random floating point value between 0 and 1.0. The Random class provides more flexible random number generating capabilities.
-
Basics
-
A random number generator is a routine designed to produce a numeric value that appears to chosen by chance. In Java, as with all programming languages, the values generated by Math.random() and the Random class appear random, but actually are not. Therefore, such methods are said to produce pseudo-random numbers.
-
Properties
-
Pseudo-random number generators have two basic properties: distribution and seed.
The "distribution" refers to the frequency in which each value in a series of generated numbers appears. Most of Java's random methods produce a uniform distribution of numbers, meaning no one value will appear more frequently than any other. The Random class has a method, nextGaussian, which generates numbers that follow a Gaussian (also "normal" or "bell curve") distribution.
The "seed" is a number used to initialize a pseudo-random number generator. Given the same seed, a pseudo-random number generator will produce an identical series of numbers when called using the same methods and parameters. The ability to reproduce a set of numbers is useful in debugging, testing and other scenarios where recreating an application's running environment is needed.
Math.random()
-
The Math.random() method is a simple pseudo-random number generator. It is used simply by calling Math.random(). The method returns a double between 0.0 (inclusive) and 1.0 (exclusive). The values produced follow a uniform distribution. If a different range, type or distribution are needed, the programmer must cast the results to the desired type and perform additional calculations to generate the desired result.
The Random Class
-
The Random class is more flexible than Math.random(), but requires a little more effort to use. An instance of the Random class is constructed. The constructor will take a long integer as the seed value, or if a seed value is not provided, it will use the current time in milliseconds as the default seed. The Random class provides a method for setting the seed after construction. There are several pseudo-random number generating methods in this class, each prefixed with the word "next." Most of these methods return uniformly distributed values across various ranges and of various types, such as integers, floating point values and Boolean values. The nextGaussian method returns Gaussian distributed doubles. The nextInt method can take an integer parameter that sets the upper end of the range.
Considerations
-
Generating a set of numbers that cannot be predicted is sometimes desirable, for example, to prevent players from being able to cheat in games. The pseudo-random values returned by several instances of the Random class can be combined to provide additional randomness in the resulting set. Implementing a nonuniform distribution also may be useful.
Java generates pseudo-random numbers that follow either a uniform or Gaussian distribution. Nonuniform distributions are useful, particularly in simulations to mimic scenarios that conform to specific distributions in reality. Other distributions can be achieved using the inverse cumulative distribution function of the desired distribution. This function takes a probability (a value between 0.0 and 1.0 exclusive) as a parameter. The nonzero results of either Math.random() or one of the Random class methods that returns a floating point value can be used for this parameter. See the Resources section for a link to the Apache Math Library, which provides inverse methods for several cumulative distributions.
References
Resources
- Photo Credit "The 19th Comes from above" is Copyrighted by Flickr user: Untitled blue (fady habib) under the Creative Commons Attribution license.