Class SobolQRNG
- All Implemented Interfaces:
Serializable
,RandomnessSource
public class SobolQRNG extends Object implements RandomnessSource
A Sobol sequence is a low-discrepancy sequence with the property that for all values of N, its subsequence (x1, ... xN) has a low discrepancy. It can be used to generate quasi-random points in a space S, which are equi-distributed. This is not a true random number generator, and is not even a pseudo-random number generator; the sequence generated from identical starting points with identical dimensions will be exactly the same. Calling this class' nextVector, nextIntVector, and nextLongVector methods all increment the position in the sequence, and do not use separate sequences for separate types.
The implementation already comes with support for up to 16 dimensions with direction numbers calculated from Stephen Joe and Frances Kuo.
The generator supports two modes:
- sequential generation of points:
nextVector()
,nextIntVector()
,nextLongVector()
, and the bounded variants on each of those - random access to the i-th point in the sequence:
skipTo(int)
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description SobolQRNG(int dimension)
Construct a new Sobol sequence generator for the given space dimension. -
Method Summary
Modifier and Type Method Description SobolQRNG
copy()
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called.boolean
equals(Object o)
double[]
fillVector(double[] toFill)
Generate a random vector.int
getNextIndex()
Returns the index i of the next point in the Sobol sequence that will be returned by callingnextVector()
.int
hashCode()
int
next(int bits)
Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.Coord
nextCoord(int xLimit, int yLimit)
Generate a random vector as a Coord, scaled between 0 (inclusive) and the given limits (exclusive).double
nextDouble()
double
nextDouble(double max)
int[]
nextIntVector()
Generate a random vector.int[]
nextIntVector(int max)
Generate a random vector.long
nextLong()
Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source.long[]
nextLongVector()
Generate a random vector.long[]
nextLongVector(long max)
Generate a random vector.double[]
nextVector()
Generate a random vector.double[]
nextVector(double max)
Generate a random vector.double[]
skipTo(int index)
Skip to the i-th point in the Sobol sequence.String
toString()
-
Constructor Details
-
SobolQRNG
Construct a new Sobol sequence generator for the given space dimension. You should callskipTo(int)
with a fairly large number (over 1000) to ensure the results aren't too obviously non-random. If you skipTo(1), all doubles in that result will be 0.5, and if you skipTo(0), all will be 0 (this class starts at index 1 instead of 0 for that reason). This is true for all dimensions.- Parameters:
dimension
- the space dimension- Throws:
ArithmeticException
- if the space dimension is outside the allowed range of [1, 16]
-
-
Method Details
-
nextVector
Generate a random vector.- Returns:
- a random vector as an array of double in the range [0.0, 1.0).
-
fillVector
Generate a random vector.- Returns:
- a random vector as an array of double in the range [0.0, 1.0).
-
nextCoord
Generate a random vector as a Coord, scaled between 0 (inclusive) and the given limits (exclusive).- Parameters:
xLimit
- the upper exclusive bound for the Coord's x-coordinateyLimit
- the upper exclusive bound for the Coord's y-coordinate- Returns:
- a random vector as a Coord between (0,0) inclusive and (xLimit,yLimit) exclusive
-
nextVector
Generate a random vector.- Parameters:
max
- the maximum exclusive value for the elements of the desired vector; minimum is 0 inclusive.- Returns:
- a random vector as an array of double.
-
nextLongVector
Generate a random vector.- Returns:
- a random vector as an array of long (only 52 bits are actually used for the result, plus sign bit).
-
nextLongVector
Generate a random vector.- Parameters:
max
- the maximum exclusive value for the elements of the desired vector; minimum is 0 inclusive.- Returns:
- a random vector as an array of long (only 52 bits are actually used for the result, plus sign bit).
-
nextIntVector
Generate a random vector.- Returns:
- a random vector as an array of int.
-
nextIntVector
Generate a random vector.- Parameters:
max
- the maximum exclusive value for the elements of the desired vector; minimum is 0 inclusive.- Returns:
- a random vector as an array of int.
-
skipTo
Skip to the i-th point in the Sobol sequence.This operation can be performed in O(1). If index is somehow negative, this uses its absolute value instead of throwing an exception. If index is 0, the result will always be entirely 0. You should skipTo a number greater than 1000 if you want random-seeming individual numbers in each vector.
- Parameters:
index
- the index in the sequence to skip to- Returns:
- the i-th point in the Sobol sequence
-
getNextIndex
Returns the index i of the next point in the Sobol sequence that will be returned by callingnextVector()
.- Returns:
- the index of the next point
-
next
Description copied from interface:RandomnessSource
Using this method, any algorithm that might use the built-in Java Random can interface with this randomness source.- Specified by:
next
in interfaceRandomnessSource
- Parameters:
bits
- the number of bits to be returned- Returns:
- a quasi-random int with at most the specified number of bits
-
nextLong
Using this method, any algorithm that needs to efficiently generate more than 32 bits of random data can interface with this randomness source. Get a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive).- Specified by:
nextLong
in interfaceRandomnessSource
- Returns:
- a random long between Long.MIN_VALUE and Long.MAX_VALUE (both inclusive)
-
nextDouble
-
nextDouble
-
copy
Produces a copy of this RandomnessSource that, if next() and/or nextLong() are called on this object and the copy, both will generate the same sequence of random numbers from the point copy() was called. This just needs to copy the state so it isn't shared, usually, and produce a new value with the same exact state.- Specified by:
copy
in interfaceRandomnessSource
- Returns:
- a copy of this RandomnessSource
-
toString
-
equals
-
hashCode
-