Class RandomNumberGenerator
java.lang.Object
nl.tudelft.simulation.jstats.streams.RandomNumberGenerator
- All Implemented Interfaces:
Serializable
,StreamInterface
- Direct Known Subclasses:
DX120Generator
,MersenneTwister
The RandomNumberGenerator class provides an abstract basis for all pseudo random number generators.
Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can be found at https://https://simulation.tudelft.nl/dsol/docs/latest/license.html.
- Author:
- Alexander Verbraeck, Peter Jacobs
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstruct a new RandomNumberGenerator.RandomNumberGenerator
(long seed) Construct a new RandomNumberGenerator. -
Method Summary
Modifier and TypeMethodDescriptionlong
Return the original seed of the generator with which it has been first initialized.long
getSeed()
Return the seed of the generator.protected abstract long
next
(int bits) returns the next value in the stream.boolean
Return the next pseudo-random, uniformly distributed boolean value.double
Return a pseudo-random number from the stream over the interval (0,1) using this stream, after advancing its state by one step.float
Return a pseudo-random number from the stream over the interval (0,1) using this stream, after advancing its state by one step.int
nextInt()
Return a pseudo-random number from the stream over using this stream, after advancing its state by one step.int
nextInt
(int i, int j) Return pseudo-random number from the stream between the integers i (inclusive) and j (inclusive).long
nextLong()
Return a pseudo-random number from the stream over using this stream, after advancing its state by one step.void
reset()
Reset the stream to use the original seed with which it was initialized.void
restoreState
(byte[] state) Restore the state to an earlier saved state object.byte[]
Save the state of the RNG into an object, e.g.abstract void
setSeed
(long seed) Set the seed of the generator.toString()
-
Field Details
-
seed
protected long seedThe seed of the generator.
-
-
Constructor Details
-
RandomNumberGenerator
public RandomNumberGenerator()Construct a new RandomNumberGenerator. The seed value used in the rng is set to System.currentTimeMillis(); -
RandomNumberGenerator
public RandomNumberGenerator(long seed) Construct a new RandomNumberGenerator.- Parameters:
seed
- long; the seed of the generator.
-
-
Method Details
-
reset
public void reset()Reset the stream to use the original seed with which it was initialized.- Specified by:
reset
in interfaceStreamInterface
-
next
protected abstract long next(int bits) returns the next value in the stream.- Parameters:
bits
- int; the number of bits used- Returns:
- the next value.
-
nextBoolean
public boolean nextBoolean()Return the next pseudo-random, uniformly distributed boolean value.- Specified by:
nextBoolean
in interfaceStreamInterface
- Returns:
- boolean; a pseudo-random boolean with 50/50 chance for true or false
-
nextDouble
public double nextDouble()Return a pseudo-random number from the stream over the interval (0,1) using this stream, after advancing its state by one step.- Specified by:
nextDouble
in interfaceStreamInterface
- Returns:
- double; the pseudo-random number
-
nextFloat
public float nextFloat()Return a pseudo-random number from the stream over the interval (0,1) using this stream, after advancing its state by one step.- Specified by:
nextFloat
in interfaceStreamInterface
- Returns:
- float; the pseudo-random number
-
nextInt
public int nextInt()Return a pseudo-random number from the stream over using this stream, after advancing its state by one step.- Specified by:
nextInt
in interfaceStreamInterface
- Returns:
- int; the pseudo-random number
-
nextInt
public int nextInt(int i, int j) Return pseudo-random number from the stream between the integers i (inclusive) and j (inclusive).- Specified by:
nextInt
in interfaceStreamInterface
- Parameters:
i
- int; the minimal valuej
- int; the maximum value- Returns:
- int; a value between i and j
-
nextLong
public long nextLong()Return a pseudo-random number from the stream over using this stream, after advancing its state by one step.- Specified by:
nextLong
in interfaceStreamInterface
- Returns:
- long; the pseudo-random number
-
setSeed
public abstract void setSeed(long seed) Set the seed of the generator.- Specified by:
setSeed
in interfaceStreamInterface
- Parameters:
seed
- long; the new seed
-
getSeed
public long getSeed()Return the seed of the generator.- Specified by:
getSeed
in interfaceStreamInterface
- Returns:
- long; the seed
-
getOriginalSeed
public long getOriginalSeed()Return the original seed of the generator with which it has been first initialized.- Specified by:
getOriginalSeed
in interfaceStreamInterface
- Returns:
- long; the original seed of the generator when it was first initialized
-
toString
-
saveState
Save the state of the RNG into an object, e.g. to roll it back to this state.- Specified by:
saveState
in interfaceStreamInterface
- Returns:
- the state as an object specific to the RNG.
- Throws:
StreamException
- when getting the state fails.
-
restoreState
Restore the state to an earlier saved state object.- Specified by:
restoreState
in interfaceStreamInterface
- Parameters:
state
- Object; the earlier saved state to which the RNG rolls back.- Throws:
StreamException
- when resetting the state fails.
-