Java Fundamental Classes Reference

Previous Chapter 17
The java.util Package
Next
 

Random

Name

Random

Synopsis

Class Name:

java.util.Random

Superclass:

java.lang.Object

Immediate Subclasses:

None

Interfaces Implemented:

java.io.Serializable

Availability:

JDK 1.0 or later

Description

The Random class is a pseudo-random number generator. Pseudo-random numbers are generated by starting with a seed value and then using an algorithm to generate a sequence of numbers that appear to be random. The Random class uses a 48-bit seed and a linear congruential algorithm to modify the seed. As a consequence of this implementation, two Random instances that are constructed with the same seed value generate exactly the same sequence of numbers.

The Random class provides methods that return pseudo-random values for various primitive Java types. The Math.random() method is easier to use if you do not need to fine-tune the generation of random numbers.

Class Summary

public class java.util.Random extends java.lang.Object
             implements java.io.Serializable {
  // Constructors
  public Random();
  public Random(long seed);
  // Instance Methods
  public void nextBytes(byte[] bytes);             // New in 1.1
  public double nextDouble();
  public float nextFloat();
  public synchronized double nextGaussian();
  public int nextInt();
  public long nextLong();
  public synchronized void setSeed(long seed);
  // Protected Instance Methods
  protected synchronized int next(int bits);       // New in 1.1
}

Constructors

Random

public Random()

Description

This constructor creates a Random object with the current time as its seed value.

public Random(long seed)

Parameters

seed

The seed value to use.

Description

This constructor creates a Random object with the given seed value.

Instance Methods

nextBytes

public void nextBytes(byte[] bytes)

Availability

New as of JDK 1.1

Parameters

bytes

The byte array to fill.

Description

This method fills the given array with pseudo-random byte values.

nextDouble

public double nextDouble()

Returns

The next pseudo-random double value.

Description

This method returns the next pseudo-random, uniformly distributed double value. The value is between 0.0 and 1.0 inclusive.

nextFloat

public float nextFloat()

Returns

The next pseudo-random float value.

Description

This method returns the next pseudo-random, uniformly distributed float value. The value is between 0.0 and 1.0 inclusive.

nextGaussian

public synchronized double nextGaussian()

Returns

The next pseudo-random double value.

Description

This method returns the next pseudo-random, Gaussian distributed double value. The value has a mean of 0.0 and a standard deviation of 1.0 from the random number sequence. The value is between -1.0 and 1.0.

nextInt

public int nextInt()

Returns

The next pseudo-random int value.

Description

This method returns the next pseudo-random, uniformly distributed int value.

nextLong

public long nextLong()

Returns

The next pseudo-random long value.

Description

This method returns the next pseudo-random, uniformly distributed long value.

setSeed

public synchronized void setSeed(long seed)

Parameters

seed

The new seed value.

Description

This method sets this Random object's seed value to the given value.

Protected Instance Methods

next

protected synchronized int next(int bits)

Availability

New as of JDK 1.1

Parameters

bits

The number of bits to generate.

Returns

The specified number of pseudo-random bits.

Description

This method generates the given number of bits and returns them as an integer value. A subclass of Random should override this method, as it is used by all of the other random-number generation methods in the class.

Inherited Methods

Method

Inherited From

Method

Inherited From

clone()

Object

equals(Object)

Object

finalize()

Object

getClass()

Object

hashCode()

Object

notify()

Object

notifyAll()

Object

toString()

Object

wait()

Object

wait(long)

Object

wait(long, int)

Object

   

See Also

Math, Serializable


Previous Home Next
PropertyResourceBundle Book Index ResourceBundle

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java
This HTML Help has been published using the chm2web software.