SSJ  3.3.1
Stochastic Simulation in Java
Public Member Functions | List of all members
RandomStreamWithCache Class Reference

This class represents a random stream whose uniforms are cached for more efficiency when using common random numbers. More...

Inheritance diagram for RandomStreamWithCache:
[legend]
Collaboration diagram for RandomStreamWithCache:
[legend]

Public Member Functions

 RandomStreamWithCache (RandomStream stream)
 Constructs a new cached random stream with internal stream stream. More...
 
 RandomStreamWithCache (RandomStream stream, int initialCapacity)
 Constructs a new cached random stream with internal stream stream. More...
 
boolean isCaching ()
 Determines if the random stream is caching values, default being true. More...
 
void setCaching (boolean caching)
 Sets the caching indicator to caching. More...
 
RandomStream getCachedStream ()
 Returns a reference to the random stream whose values are cached. More...
 
void setCachedStream (RandomStream stream)
 Sets the random stream whose values are cached to stream. More...
 
void clearCache ()
 Clears the cached values for this random stream. More...
 
void initCache ()
 Resets this random stream to recover values from the cache. More...
 
int getNumCachedValues ()
 Returns the total number of values cached by this random stream. More...
 
int getCacheIndex ()
 Return the index of the next cached value that will be returned by the stream. More...
 
void setCacheIndex (int newIndex)
 Sets the index, in the cache, of the next value returned by nextDouble. More...
 
DoubleArrayList getCachedValues ()
 Returns an array list containing the values cached by this random stream. More...
 
void setCachedValues (DoubleArrayList values)
 Sets the array list containing the cached values to values. More...
 
void resetStartStream ()
 Reinitializes the stream to its initial state \(I_g\): \(C_g\) and \(B_g\) are set to \(I_g\).
 
void resetStartSubstream ()
 Reinitializes the stream to the beginning of its current substream: \(C_g\) is set to \(B_g\).
 
void resetNextSubstream ()
 Reinitializes the stream to the beginning of its next substream: \(N_g\) is computed, and \(C_g\) and \(B_g\) are set to \(N_g\).
 
double nextDouble ()
 Returns a (pseudo)random number from the uniform distribution over the interval \((0,1)\), using this stream, after advancing its state by one step. More...
 
void nextArrayOfDouble (double[] u, int start, int n)
 Generates n (pseudo)random numbers from the uniform distribution and stores them into the array u starting at index start. More...
 
int nextInt (int i, int j)
 Returns a (pseudo)random number from the discrete uniform distribution over the integers \(\{i,i+1,…,j\}\), using this stream. More...
 
void nextArrayOfInt (int i, int j, int[] u, int start, int n)
 Generates n (pseudo)random numbers from the discrete uniform distribution over the integers \(\{i,i+1,…,j\}\), using this stream and stores the result in the array u starting at index start. More...
 
- Public Member Functions inherited from RandomStream
String toString ()
 Returns a string containing the current state of this stream. More...
 

Detailed Description

This class represents a random stream whose uniforms are cached for more efficiency when using common random numbers.

An object from this class is constructed with a reference to a RandomStream instance used to get the random numbers. These numbers are stored in an internal array to be retrieved later. The dimension of the array increases as the values are generated. If the nextDouble method is called after the object is reset, it gives back the cached values instead of computing new ones. If the cache is exhausted before the stream is reset, new values are computed, and added to the cache.

Such caching allows for a better performance with common random numbers, when generating uniforms is time-consuming. It can also help with restoring the simulation to a certain state without setting stream-specific seeds. However, using such caching may lead to memory problems if a large quantity of random numbers are needed.

Constructor & Destructor Documentation

◆ RandomStreamWithCache() [1/2]

Constructs a new cached random stream with internal stream stream.

Parameters
streamthe random stream whose uniforms are cached.
Exceptions
NullPointerExceptionif stream is null.

◆ RandomStreamWithCache() [2/2]

RandomStreamWithCache ( RandomStream  stream,
int  initialCapacity 
)

Constructs a new cached random stream with internal stream stream.

The initialCapacity parameter is used to set the initial capacity of the internal array which can grow as needed; it does not limit the total size of the cache.

Parameters
streamthe random stream whose values are cached.
initialCapacitythe initial capacity of the cache.
Exceptions
NullPointerExceptionif stream is null.

Member Function Documentation

◆ clearCache()

void clearCache ( )

Clears the cached values for this random stream.

Any subsequent call will then obtain new values from the internal stream.

◆ getCachedStream()

RandomStream getCachedStream ( )

Returns a reference to the random stream whose values are cached.

Returns
a reference to the random stream whose values are cached.

◆ getCachedValues()

DoubleArrayList getCachedValues ( )

Returns an array list containing the values cached by this random stream.

Returns
the array of cached values.

◆ getCacheIndex()

int getCacheIndex ( )

Return the index of the next cached value that will be returned by the stream.

If the cache is exhausted, the returned value corresponds to the value returned by getNumCachedValues, and a subsequent call to nextDouble will generate a new variate rather than reading a previous one from the cache. If caching is disabled, this always returns 0.

Returns
the index of the next cached value.

◆ getNumCachedValues()

int getNumCachedValues ( )

Returns the total number of values cached by this random stream.

Returns
the total number of cached values.

◆ initCache()

void initCache ( )

Resets this random stream to recover values from the cache.

Subsequent calls to nextDouble will return the cached uniforms until all the values are returned. When the array of cached values is exhausted, the internal random stream is used to generate new values which are added to the internal array as well. This method is equivalent to calling setCacheIndex(int).

◆ isCaching()

boolean isCaching ( )

Determines if the random stream is caching values, default being true.

When caching is turned OFF, the nextDouble method simply calls the corresponding method on the internal random stream, without storing the generated uniforms.

Returns
the caching indicator.

◆ nextArrayOfDouble()

void nextArrayOfDouble ( double []  u,
int  start,
int  n 
)

Generates n (pseudo)random numbers from the uniform distribution and stores them into the array u starting at index start.

Parameters
uarray that will contain the generated uniforms
startstarting index, in the array u, to write uniforms from
nnumber of uniforms to generate

Implements RandomStream.

◆ nextArrayOfInt()

void nextArrayOfInt ( int  i,
int  j,
int []  u,
int  start,
int  n 
)

Generates n (pseudo)random numbers from the discrete uniform distribution over the integers \(\{i,i+1,…,j\}\), using this stream and stores the result in the array u starting at index start.

(Calls nextInt n times.)

Parameters
ismallest integer that can be generated
jgreatest integer that can be generated
uarray that will contain the generated values
startstarting index, in the array u, to write integers from
nnumber of values being generated

Implements RandomStream.

◆ nextDouble()

double nextDouble ( )

Returns a (pseudo)random number from the uniform distribution over the interval \((0,1)\), using this stream, after advancing its state by one step.

The generators programmed in SSJ never return the values 0 or 1.

Returns
the next generated uniform

Implements RandomStream.

◆ nextInt()

int nextInt ( int  i,
int  j 
)

Returns a (pseudo)random number from the discrete uniform distribution over the integers \(\{i,i+1,…,j\}\), using this stream.

(Calls nextDouble once.)

Parameters
ismallest integer that can be generated
jgreatest integer that can be generated
Returns
the generated integer

Implements RandomStream.

◆ setCachedStream()

void setCachedStream ( RandomStream  stream)

Sets the random stream whose values are cached to stream.

If the stream is changed, the clearCache method is called to clear the cache.

Parameters
streamthe new random stream whose values will be cached.
Exceptions
NullPointerExceptionif stream is null.

◆ setCachedValues()

void setCachedValues ( DoubleArrayList  values)

Sets the array list containing the cached values to values.

This resets the cache index to the size of the given array.

Parameters
valuesthe array list of cached values.
Exceptions
NullPointerExceptionif values is null.

◆ setCacheIndex()

void setCacheIndex ( int  newIndex)

Sets the index, in the cache, of the next value returned by nextDouble.

If newIndex is 0, this is equivalent to calling initCache. If newIndex is getNumCachedValues, subsequent calls to nextDouble will add new values to the cache.

Parameters
newIndexthe new index.
Exceptions
IllegalArgumentExceptionif newIndex is negative or greater than or equal to the cache size.

◆ setCaching()

void setCaching ( boolean  caching)

Sets the caching indicator to caching.

If caching is turned OFF, this method calls clearCache to clear the cached values.

Parameters
cachingthe new value of the caching indicator.

The documentation for this class was generated from the following file: