SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
umontreal.ssj.randvar.RandomVariateGenWithCache Class Reference

This class represents a random variate generator whose values are cached for more efficiency when using common random numbers. More...

Inheritance diagram for umontreal.ssj.randvar.RandomVariateGenWithCache:
umontreal.ssj.randvar.RandomVariateGen

Public Member Functions

 RandomVariateGenWithCache (RandomVariateGen rvg)
 Constructs a new cached random variate generator with internal generator rvg.
 RandomVariateGenWithCache (RandomVariateGen rvg, int initialCapacity)
 Constructs a new cached random variate generator with internal generator rvg.
boolean isCaching ()
 Determines if the random variate generator is caching values, default being true.
void setCaching (boolean caching)
 Sets the caching indicator to caching.
RandomVariateGen getCachedGen ()
 Returns a reference to the random variate generator whose values are cached.
void setCachedGen (RandomVariateGen rvg)
 Sets the random variate generator whose values are cached to rvg.
void clearCache ()
 Clears the cached values for this cached generator.
void initCache ()
 Resets this generator to recover values from the cache.
int getNumCachedValues ()
 Returns the total number of values cached by this generator.
int getCacheIndex ()
 Return the index of the next cached value that will be returned by the generator.
void setCacheIndex (int newIndex)
 Sets the index, in the cache, of the next value returned by nextDouble.
DoubleArrayList getCachedValues ()
 Returns an array list containing the values cached by this random variate generator.
void setCachedValues (DoubleArrayList values)
 Sets the array list containing the cached values to values.
double nextDouble ()
 Generates a random number from the continuous distribution contained in this object.
void nextArrayOfDouble (double[] v, int start, int n)
 Generates n random numbers from the continuous distribution contained in this object.
RandomStream getStream ()
 Returns the umontreal.ssj.rng.RandomStream used by this generator.
Distribution getDistribution ()
 Returns the umontreal.ssj.probdist.Distribution used by this generator.
Public Member Functions inherited from umontreal.ssj.randvar.RandomVariateGen
 RandomVariateGen (RandomStream s, Distribution dist)
 Creates a new random variate generator from the distribution dist, using stream s.
double[] nextArrayOfDouble (int n)
 Generates n random numbers from the continuous distribution contained in this object, and returns them in a new array of size n.
void setStream (RandomStream stream)
 Sets the umontreal.ssj.rng.RandomStream used by this generator to stream.
String toString ()
 Returns a String containing information about the current generator.

Detailed Description

This class represents a random variate generator whose values are cached for more efficiency when using common random numbers.

An object from this class is constructed with a reference to a RandomVariateGen 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 (by calling setCachedValues(DoubleArrayList) ), it gives back the cached values instead of computing new ones. If the cache is exhausted before the generator is reset, new values are computed and added to the cache.

Such caching allows for a better performance with common random numbers, when generating random variates is time-consuming. However, using such caching may lead to memory problems if a large quantity of random numbers are needed.

Definition at line 51 of file RandomVariateGenWithCache.java.

Constructor & Destructor Documentation

◆ RandomVariateGenWithCache() [1/2]

umontreal.ssj.randvar.RandomVariateGenWithCache.RandomVariateGenWithCache ( RandomVariateGen rvg)

Constructs a new cached random variate generator with internal generator rvg.

Parameters
rvgthe random variate generator whose values are cached.
Exceptions
NullPointerExceptionif `rvg` is `null`.

Definition at line 64 of file RandomVariateGenWithCache.java.

◆ RandomVariateGenWithCache() [2/2]

umontreal.ssj.randvar.RandomVariateGenWithCache.RandomVariateGenWithCache ( RandomVariateGen rvg,
int initialCapacity )

Constructs a new cached random variate generator with internal generator rvg.

The initialCapacity parameter is used to set the initial capacity of the internal array which can grow as needed; it does not limit the maximal number of cached values.

Parameters
rvgthe random variate generator whose values are cached.
initialCapacitythe number of cached values.
Exceptions
NullPointerExceptionif `rvg` is `null`.

Definition at line 81 of file RandomVariateGenWithCache.java.

Member Function Documentation

◆ clearCache()

void umontreal.ssj.randvar.RandomVariateGenWithCache.clearCache ( )

Clears the cached values for this cached generator.

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

Definition at line 141 of file RandomVariateGenWithCache.java.

◆ getCachedGen()

RandomVariateGen umontreal.ssj.randvar.RandomVariateGenWithCache.getCachedGen ( )

Returns a reference to the random variate generator whose values are cached.

Returns
a reference to the random variate generator whose values are cached.

Definition at line 117 of file RandomVariateGenWithCache.java.

◆ getCachedValues()

DoubleArrayList umontreal.ssj.randvar.RandomVariateGenWithCache.getCachedValues ( )

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

Returns
the array of cached values.

Definition at line 206 of file RandomVariateGenWithCache.java.

◆ getCacheIndex()

int umontreal.ssj.randvar.RandomVariateGenWithCache.getCacheIndex ( )

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

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.

Definition at line 180 of file RandomVariateGenWithCache.java.

◆ getDistribution()

Distribution umontreal.ssj.randvar.RandomVariateGenWithCache.getDistribution ( )

Returns the umontreal.ssj.probdist.Distribution used by this generator.

Returns
the distribution associated to that object

Reimplemented from umontreal.ssj.randvar.RandomVariateGen.

Definition at line 263 of file RandomVariateGenWithCache.java.

◆ getNumCachedValues()

int umontreal.ssj.randvar.RandomVariateGenWithCache.getNumCachedValues ( )

Returns the total number of values cached by this generator.

Returns
the total number of cached values.

Definition at line 167 of file RandomVariateGenWithCache.java.

◆ getStream()

RandomStream umontreal.ssj.randvar.RandomVariateGenWithCache.getStream ( )

Returns the umontreal.ssj.rng.RandomStream used by this generator.

Returns
the stream associated to this object

Reimplemented from umontreal.ssj.randvar.RandomVariateGen.

Definition at line 259 of file RandomVariateGenWithCache.java.

◆ initCache()

void umontreal.ssj.randvar.RandomVariateGenWithCache.initCache ( )

Resets this generator to recover values from the cache.

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

Definition at line 158 of file RandomVariateGenWithCache.java.

◆ isCaching()

boolean umontreal.ssj.randvar.RandomVariateGenWithCache.isCaching ( )

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

When caching is turned OFF, the nextDouble method simply calls the corresponding method on the internal random variate generator, without storing the generated values.

Returns
the caching indicator.

Definition at line 96 of file RandomVariateGenWithCache.java.

◆ nextArrayOfDouble()

void umontreal.ssj.randvar.RandomVariateGenWithCache.nextArrayOfDouble ( double[] v,
int start,
int n )

Generates n random numbers from the continuous distribution contained in this object.

These numbers are stored in the array v, starting from index start. By default, this method calls nextDouble() n times, but one can override it in subclasses for better efficiency.

Parameters
varray in which the variates will be stored
startstarting index, in v, of the new variates
nnumber of variates to generate

Reimplemented from umontreal.ssj.randvar.RandomVariateGen.

Definition at line 236 of file RandomVariateGenWithCache.java.

◆ nextDouble()

double umontreal.ssj.randvar.RandomVariateGenWithCache.nextDouble ( )

Generates a random number from the continuous distribution contained in this object.

By default, this method uses inversion by calling the umontreal.ssj.probdist.ContinuousDistribution.inverseF method of the distribution object. Alternative generating methods are provided in subclasses.

Returns
the generated value

Reimplemented from umontreal.ssj.randvar.RandomVariateGen.

Definition at line 224 of file RandomVariateGenWithCache.java.

◆ setCachedGen()

void umontreal.ssj.randvar.RandomVariateGenWithCache.setCachedGen ( RandomVariateGen rvg)

Sets the random variate generator whose values are cached to rvg.

If the generator is changed, the clearCache method is called.

Parameters
rvgthe new random variate generator whose values are cached.
Exceptions
NullPointerExceptionif `rvg` is `null`.

Definition at line 128 of file RandomVariateGenWithCache.java.

◆ setCachedValues()

void umontreal.ssj.randvar.RandomVariateGenWithCache.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`.

Definition at line 217 of file RandomVariateGenWithCache.java.

◆ setCacheIndex()

void umontreal.ssj.randvar.RandomVariateGenWithCache.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.

Definition at line 194 of file RandomVariateGenWithCache.java.

◆ setCaching()

void umontreal.ssj.randvar.RandomVariateGenWithCache.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.

Definition at line 106 of file RandomVariateGenWithCache.java.


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