Top Description Fields Constructors Methods
org.apache.commons.math3.random

public abstract Class AbstractRandomGenerator

extends Object
implements RandomGenerator
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.random.RandomGenerator
Imports
org.apache.commons.math3.exception.NotStrictlyPositiveException, org.apache.commons.math3.util.FastMath

Abstract class implementing the RandomGenerator interface. Default implementations for all methods other than nextDouble() and setSeed(long) are provided.

All data generation methods are based on code nextDouble(). Concrete implementations must override this method and should provide better / more performant implementations of the other methods if the underlying PRNG supplies them.

Since
1.1

Field Summary

Modifier and TypeField and Description
private double
cachedNormalDeviate

Cached random normal value.

Constructor Summary

AccessConstructor and Description
public
AbstractRandomGenerator()

Construct a RandomGenerator.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Clears the cache used by the default implementation of nextGaussian.

public boolean

Returns:

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

Implements org.apache.commons.math3.random.RandomGenerator.nextBoolean.

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

public void
nextBytes(byte[]
the non-null byte array in which to put the random bytes
bytes
)

Implements org.apache.commons.math3.random.RandomGenerator.nextBytes.

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

public abstract double

Returns:

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

Redeclares org.apache.commons.math3.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.0 and 1.0 from this random number generator's sequence
nextFloat
()

Implements org.apache.commons.math3.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 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
()

Implements org.apache.commons.math3.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
()

Implements org.apache.commons.math3.random.RandomGenerator.nextInt.

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

public int

Returns:

a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
nextInt
(int
the bound on the random number to be returned. Must be positive.
n
)

Implements org.apache.commons.math3.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 org.apache.commons.math3.random.RandomGenerator.nextLong.

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

public void
setSeed(int
the seed value
seed
)

Implements org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using an int seed.

public void
setSeed(int[]
the seed value
seed
)

Implements org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using an int array seed.

public abstract void
setSeed(long
the seed value
seed
)

Redeclares org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using a long seed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachedNormalDeviateback to summary
private double cachedNormalDeviate

Cached random normal value. The default implementation for nextGaussian generates pairs of values and this field caches the second value so that the full algorithm is not executed for every activation. The value Double.NaN signals that there is no cached value. Use clear to clear the cached value.

Constructor Detail

AbstractRandomGeneratorback to summary
public AbstractRandomGenerator()

Construct a RandomGenerator.

Method Detail

clearback to summary
public void clear()

Clears the cache used by the default implementation of nextGaussian. Implementations that do not override the default implementation of nextGaussian should call this method in the implementation of setSeed(long)

nextBooleanback to summary
public boolean nextBoolean()

Implements org.apache.commons.math3.random.RandomGenerator.nextBoolean.

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

The default implementation returns

nextDouble() <= 0.5

Returns:boolean

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

nextBytesback to summary
public void nextBytes(byte[] bytes)

Implements org.apache.commons.math3.random.RandomGenerator.nextBytes.

Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.

The default implementation fills the array with bytes extracted from random integers generated using nextInt.

Parameters
bytes:byte[]

the non-null byte array in which to put the random bytes

nextDoubleback to summary
public abstract double nextDouble()

Redeclares org.apache.commons.math3.random.RandomGenerator.nextDouble.

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

This method provides the underlying source of random data used by the other methods.

Returns:double

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

nextFloatback to summary
public float nextFloat()

Implements org.apache.commons.math3.random.RandomGenerator.nextFloat.

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

The default implementation returns

(float) nextDouble() 

Returns:float

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

nextGaussianback to summary
public double nextGaussian()

Implements org.apache.commons.math3.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.

The default implementation uses the Polar Method due to G.E.P. Box, M.E. Muller and G. Marsaglia, as described in D. Knuth, The Art of Computer Programming, 3.4.1C.

The algorithm generates a pair of independent random values. One of these is cached for reuse, so the full algorithm is not executed on each activation. Implementations that do not override this method should make sure to call clear to clear the cached value in the implementation of setSeed(long).

Returns:double

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

nextIntback to summary
public int nextInt()

Implements org.apache.commons.math3.random.RandomGenerator.nextInt.

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. All 232 possible int values should be produced with (approximately) equal probability.

The default implementation provided here returns

(int) (nextDouble() * Integer.MAX_VALUE)

Returns:int

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

nextIntback to summary
public int nextInt(int n)

Implements org.apache.commons.math3.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.

The default implementation returns

(int) (nextDouble() * n

Parameters
n:int

the bound on the random number to be returned. Must be positive.

Returns:int

a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).

Exceptions
NotStrictlyPositiveException:
if n <= 0.
nextLongback to summary
public long nextLong()

Implements org.apache.commons.math3.random.RandomGenerator.nextLong.

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. All 264 possible long values should be produced with (approximately) equal probability.

The default implementation returns

 (long) (nextDouble() * Long.MAX_VALUE)
 

Returns:long

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

setSeedback to summary
public void setSeed(int seed)

Implements org.apache.commons.math3.random.RandomGenerator.setSeed.

Doc from org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using an int seed.

Sequences of values generated starting with the same seeds should be identical.

Parameters
seed:int

the seed value

setSeedback to summary
public void setSeed(int[] seed)

Implements org.apache.commons.math3.random.RandomGenerator.setSeed.

Doc from org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using an int array seed.

Sequences of values generated starting with the same seeds should be identical.

Parameters
seed:int[]

the seed value

setSeedback to summary
public abstract void setSeed(long seed)

Redeclares org.apache.commons.math3.random.RandomGenerator.setSeed.

Sets the seed of the underlying random number generator using a long seed. Sequences of values generated starting with the same seeds should be identical.

Implementations that do not override the default implementation of nextGaussian should include a call to clear in the implementation of this method.

Parameters
seed:long

the seed value