Name

Random

Synopsis

This class implements a pseudorandom number generator suitable for games and similar applications. If you need a cryptographic-strength source of pseudorandomness, see java.security.SecureRandom. nextDouble( ) and nextFloat( ) return a value between 0.0 and 1.0. nextLong( ) and the no-argument version of nextInt( ) return long and int values distributed across the range of those data types. As of Java 1.2, if you pass an argument to nextInt( ), it returns a value between zero (inclusive) and the specified number (exclusive). nextGaussian( ) returns pseudorandom floating-point values with a Gaussian distribution; the mean of the values is 0.0 and the standard deviation is 1.0. nextBoolean( ) returns a pseudorandom boolean value, and nextBytes( ) fills in the specified byte array with pseudorandom bytes. You can use the setSeed( ) method or the optional constructor argument to initialize the pseudorandom number generator with some variable seed value other than the current time (the default) or with a constant to ensure a repeatable sequence of pseudorandomness.

java.util.Random

Figure 16-52. java.util.Random

public class Random implements Serializable {
// Public Constructors
     public Random( );  
     public Random(long seed);  
// Public Instance Methods
                  1.2  public boolean nextBoolean( );  
1.1  public void nextBytes(byte[ ] bytes);  
     public double nextDouble( );  
     public float nextFloat( ); ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.