Packages  This Package  Prev  Next  Index  

§3.7 Class Random

public  class  java.util.Random
    extends  java.lang.Object  (I-§1.12)
{
        // Constructors
    public Random();	§3.7.1
    public Random(long  seed);	§3.7.2

        // Methods
    public double nextDouble();	§3.7.3
    public float nextFloat();	§3.7.4
    public double nextGaussian();	§3.7.5
    public int nextInt();	§3.7.6
    public long nextLong();	§3.7.7
    public void setSeed(long  seed);	§3.7.8
}
An instance of this class is used to generate a stream of pseudo-random numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

Many applications will find the random method (I-§1.10.26) in class Math simpler to use.


Constructors

Random

public Random()
Creates a new random number generator. Its seed is initialized to a value based on the current time (I-§1.18.5).

Random

public Random(long seed)
Creates a new random number generator using a single long seed.
Parameters:
seed - the initial seed
See Also:
setSeed (I-§3.7.8).

Methods

nextDouble

public double nextDouble()
Returns:
the next pseudorandom, uniformally distributed double value between 0.0 and 1.0 from this random number generator's sequence.

nextFloat

public float nextFloat()
Returns:
the next pseudorandom, uniformally distributed float value between 0.0 and 1.0 from this random number generator's sequence.

nextGaussian

public double nextGaussian()
Returns:
the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

nextInt

public int nextInt()
Returns:
the next pseudorandom, uniformally distributed int value from this random number generator's sequence.

nextLong

public long nextLong()
Returns:
the nextpseudorandom, uniformally distributed long value from this random number generator's sequence.

setSeed

public void setSeed(long seed)
Sets the seed of this random number generator using a single long seed.
Parameters:
seed - the initial seed

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com