Top Description Inners Fields Constructors Methods
java.util

public Class Random

extends Object
implements RandomGenerator, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.random.RandomGenerator
Known Direct Subclasses
java.util.Random.RandomWrapper, java.security.SecureRandom, java.util.concurrent.ThreadLocalRandom, java.util.concurrent.ThreadLocalRandom.ThreadLocalRandomProxy
Imports
java.io.*, java.util.concurrent.atomic.AtomicLong, java.util.random.RandomGenerator, java.util.stream.DoubleStream, .IntStream, .LongStream, jdk.internal.misc.Unsafe

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 248. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms, 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. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the method Math#random simpler to use.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using java.util.concurrent.ThreadLocalRandom in multithreaded designs.

Instances of java.util.Random are not cryptographically secure. Consider instead using java.security.SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.

Author
Frank Yellin
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final long
private static final double
private static final float
private boolean
private static final long
private static final long
private double
private final AtomicLong
seed

The internal state associated with this pseudorandom number generator.

private static final long
private static final AtomicLong
private static final ObjectStreamField[]
serialPersistentFields

Serializable fields for Random.

pack-priv static final long
serialVersionUID

use serialVersionUID from JDK 1.1 for interoperability

private static final Unsafe

Constructor Summary

AccessConstructor and Description
public
Random()

Creates a new random number generator.

private
Random(Void unused)

public
Random(long
the initial seed
seed
)

Creates a new random number generator using a single long seed.

Method Summary

Modifier and TypeMethod and Description
public DoubleStream

Returns:

a stream of double values
doubles
(long
the number of values to generate
streamSize
)

Overrides default java.util.random.RandomGenerator.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).

public DoubleStream

Returns:

a stream of pseudorandom double values
doubles
()

Overrides default java.util.random.RandomGenerator.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

public DoubleStream

Returns:

a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
doubles
(long
the number of values to generate
streamSize
,
double
the origin (inclusive) of each random value
randomNumberOrigin
,
double
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.doubles.

Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

public DoubleStream

Returns:

a stream of pseudorandom double values, each with the given origin (inclusive) and bound (exclusive)
doubles
(double
the origin (inclusive) of each random value
randomNumberOrigin
,
double
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.doubles.

Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

public static Random

Returns:

the delegating Random instance
from
(RandomGenerator
the RandomGenerator calls are delegated to
generator
)

Returns an instance of Random that delegates method calls to the RandomGenerator argument.

private static long
initialScramble(long seed)

public IntStream

Returns:

a stream of pseudorandom int values
ints
(long
the number of values to generate
streamSize
)

Overrides default java.util.random.RandomGenerator.ints.

Returns a stream producing the given streamSize number of pseudorandom int values.

public IntStream

Returns:

a stream of pseudorandom int values
ints
()

Overrides default java.util.random.RandomGenerator.ints.

Returns an effectively unlimited stream of pseudorandom int values.

public IntStream

Returns:

a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
ints
(long
the number of values to generate
streamSize
,
int
the origin (inclusive) of each random value
randomNumberOrigin
,
int
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.ints.

Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

public IntStream

Returns:

a stream of pseudorandom int values, each with the given origin (inclusive) and bound (exclusive)
ints
(int
the origin (inclusive) of each random value
randomNumberOrigin
,
int
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.ints.

Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

public LongStream

Returns:

a stream of pseudorandom long values
longs
(long
the number of values to generate
streamSize
)

Overrides default java.util.random.RandomGenerator.longs.

Returns a stream producing the given streamSize number of pseudorandom long values.

public LongStream

Returns:

a stream of pseudorandom long values
longs
()

Overrides default java.util.random.RandomGenerator.longs.

Returns an effectively unlimited stream of pseudorandom long values.

public LongStream

Returns:

a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
longs
(long
the number of values to generate
streamSize
,
long
the origin (inclusive) of each random value
randomNumberOrigin
,
long
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.longs.

Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).

public LongStream

Returns:

a stream of pseudorandom long values, each with the given origin (inclusive) and bound (exclusive)
longs
(long
the origin (inclusive) of each random value
randomNumberOrigin
,
long
the bound (exclusive) of each random value
randomNumberBound
)

Overrides default java.util.random.RandomGenerator.longs.

Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).

protected int

Returns:

the next pseudorandom value from this random number generator's sequence
next
(int
random bits
bits
)

Generates the next pseudorandom number.

public boolean

Returns:

the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence
nextBoolean
()

Overrides default java.util.random.RandomGenerator.nextBoolean.

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.

public void
nextBytes(byte[]
the byte array to fill with random bytes
bytes
)

Overrides default java.util.random.RandomGenerator.nextBytes.

Generates random bytes and places them into a user-supplied byte array.

public double

Returns:

the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence
nextDouble
()

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

public float

Returns:

the next pseudorandom, uniformly distributed float value between 0.0f and 1.0f from this random number generator's sequence
nextFloat
()

Overrides default java.util.random.RandomGenerator.nextFloat.

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

public synchronized double

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
nextGaussian
()

Overrides default java.util.random.RandomGenerator.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.

public int

Returns:

the next pseudorandom, uniformly distributed int value from this random number generator's sequence
nextInt
()

Overrides default java.util.random.RandomGenerator.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

public int

Returns:

the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence
nextInt
(int
the upper bound (exclusive). Must be positive.
bound
)

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

public long

Returns:

the next pseudorandom, uniformly distributed long value from this random number generator's sequence
nextLong
()

Implements java.util.random.RandomGenerator.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

private void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
s
)

Reconstitute the Random instance from a stream (that is, deserialize it).

private void
resetSeed(long seedVal)

private static long
public synchronized void
setSeed(long
the seed value
seed
)

Sets or updates the seed of this random number generator using the provided long seed value (optional operation).

private synchronized void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
s
)

Save the Random instance to a stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait