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.
in multithreaded
designs.
Instances of java.util.Random
are not cryptographically
secure. Consider instead using java.
to
get a cryptographically secure pseudo-random number generator for use
by security-sensitive applications.
Modifier and Type | Class and Description |
---|---|
private static class | Random.
Class used to wrap a |
Modifier and Type | Field 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 |
Access | Constructor and Description |
---|---|
public | |
private | |
public |
Modifier and Type | Method and Description |
---|---|
public DoubleStream | Returns: a stream ofdouble valuesthe number of values to generate streamSize)Overrides default java. Returns a stream producing the given |
public DoubleStream | Returns: a stream of pseudorandomdouble valuesOverrides default java. Returns an effectively unlimited stream of pseudorandom |
public DoubleStream | Returns: a stream of pseudorandomdouble values,
each with the given origin (inclusive) and bound (exclusive)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. Returns a stream producing the given |
public DoubleStream | Returns: a stream of pseudorandomdouble values,
each with the given origin (inclusive) and bound (exclusive)the origin (inclusive) of each random value randomNumberOrigin, double the bound (exclusive) of each random value randomNumberBound)Overrides default java. Returns an effectively unlimited stream of pseudorandom |
public static Random | Returns: the delegatingRandom instancethe generator)RandomGenerator calls are delegated toReturns an instance of |
private static long | |
public IntStream | Returns: a stream of pseudorandomint valuesthe number of values to generate streamSize)Overrides default java. Returns a stream producing the given |
public IntStream | Returns: a stream of pseudorandomint valuesOverrides default java. Returns an effectively unlimited stream of pseudorandom |
public IntStream | Returns: a stream of pseudorandomint values,
each with the given origin (inclusive) and bound (exclusive)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. Returns a stream producing the given |
public IntStream | Returns: a stream of pseudorandomint values,
each with the given origin (inclusive) and bound (exclusive)the origin (inclusive) of each random value randomNumberOrigin, int the bound (exclusive) of each random value randomNumberBound)Overrides default java. Returns an effectively unlimited stream of pseudorandom |
public LongStream | Returns: a stream of pseudorandomlong valuesthe number of values to generate streamSize)Overrides default java. Returns a stream producing the given |
public LongStream | Returns: a stream of pseudorandomlong valuesOverrides default java. Returns an effectively unlimited stream of pseudorandom |
public LongStream | Returns: a stream of pseudorandomlong values,
each with the given origin (inclusive) and bound (exclusive)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. Returns a stream producing the given |
public LongStream | Returns: a stream of pseudorandomlong values,
each with the given origin (inclusive) and bound (exclusive)the origin (inclusive) of each random value randomNumberOrigin, long the bound (exclusive) of each random value randomNumberBound)Overrides default java. Returns an effectively unlimited stream of pseudorandom |
protected int | Returns: the next pseudorandom value from this random number generator's sequencerandom bits bits)Generates the next pseudorandom number. |
public boolean | Returns: the next pseudorandom, uniformly distributedboolean value from this random number generator's
sequenceOverrides default java. Returns the next pseudorandom, uniformly distributed
|
public void | nextBytes(byte[]
the byte array to fill with random bytes bytes)Overrides default java. Generates random bytes and places them into a user-supplied byte array. |
public double | Returns: the next pseudorandom, uniformly distributeddouble
value between 0.0 and 1.0 from this
random number generator's sequenceOverrides default java. Returns the next pseudorandom, uniformly distributed
|
public float | Returns: the next pseudorandom, uniformly distributedfloat
value between 0.0f and 1.0f from this
random number generator's sequenceOverrides default java. Returns the next pseudorandom, uniformly distributed |
public synchronized double | Returns: the next pseudorandom, Gaussian ("normally") distributeddouble value with mean 0.0 and
standard deviation 1.0 from this random number
generator's sequenceOverrides default java. Returns the next pseudorandom, Gaussian ("normally") distributed
|
public int | Returns: the next pseudorandom, uniformly distributedint
value from this random number generator's sequenceOverrides default java. Returns the next pseudorandom, uniformly distributed |
public int | Returns: the next pseudorandom, uniformly distributedint
value between zero (inclusive) and bound (exclusive)
from this random number generator's sequencethe upper bound (exclusive). Must be positive. bound)Overrides default java. Returns a pseudorandom, uniformly distributed |
public long | Returns: the next pseudorandom, uniformly distributedlong
value from this random number generator's sequenceImplements java. Returns the next pseudorandom, uniformly distributed |
private void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readReconstitute the |
private void | |
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 |
private synchronized void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenSave the |