public class RandomVariateGenWithCache extends RandomVariateGen
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), 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.
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clearCache()
Clears the cached values for this cached generator.
|
RandomVariateGen |
getCachedGen()
Returns a reference to the random variate generator
whose values are cached.
|
DoubleArrayList |
getCachedValues()
Returns an array list containing the values
cached by this random variate generator.
|
int |
getCacheIndex()
Return the index of the next cached value that will be
returned by the generator.
|
Distribution |
getDistribution()
Returns the
Distribution used by this generator. |
int |
getNumCachedValues()
Returns the total number of values cached by this generator.
|
RandomStream |
getStream()
Returns the
RandomStream used by this generator. |
void |
initCache()
Resets this generator to recover values from the cache.
|
boolean |
isCaching()
Determines if the random variate generator is caching values,
default being true.
|
void |
nextArrayOfDouble(double[] v,
int start,
int n)
Generates n random numbers from the continuous distribution
contained in this object.
|
double |
nextDouble()
Generates a random number from the continuous distribution
contained in this object.
|
void |
setCachedGen(RandomVariateGen rvg)
Sets the random variate generator whose values are cached to
rvg.
|
void |
setCachedValues(DoubleArrayList values)
Sets the array list containing the cached
values to values.
|
void |
setCacheIndex(int newIndex)
Sets the index, in the cache, of the next value returned
by
nextDouble. |
void |
setCaching(boolean caching)
Sets the caching indicator to caching.
|
setStreampublic RandomVariateGenWithCache(RandomVariateGen rvg)
rvg - the random variate generator whose values are cached.java.lang.NullPointerException - if rvg is null.public RandomVariateGenWithCache(RandomVariateGen rvg, int initialCapacity)
rvg - the random variate generator whose values are cached.initialCapacity - the number of cached values.java.lang.NullPointerException - if rvg is null.public boolean isCaching()
nextDouble
method simply calls the corresponding method on the internal
random variate generator, without storing the generated values.public void setCaching(boolean caching)
clearCache
to clear the cached values.caching - the new value of the caching indicator.public RandomVariateGen getCachedGen()
public void setCachedGen(RandomVariateGen rvg)
clearCache
method is called.rvg - the new random variate generator whose values are cached.java.lang.NullPointerException - if rvg is null.public void clearCache()
public void initCache()
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.public int getNumCachedValues()
public int getCacheIndex()
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.public void setCacheIndex(int newIndex)
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.newIndex - the new index.java.lang.IllegalArgumentException - if newIndex
is negative or greater than or equal to the cache size.public DoubleArrayList getCachedValues()
public void setCachedValues(DoubleArrayList values)
values - the array list of cached values.java.lang.NullPointerException - if values is null.public double nextDouble()
RandomVariateGeninverseF
method of the distribution object.
Alternative generating methods are provided in subclasses.nextDouble in class RandomVariateGenpublic void nextArrayOfDouble(double[] v,
int start,
int n)
RandomVariateGennextDouble() n
times, but one can override it in subclasses for better efficiency.nextArrayOfDouble in class RandomVariateGenv - array in which the variates will be storedstart - starting index, in v, of the new variatesn - number of variates to generatepublic RandomStream getStream()
RandomVariateGenRandomStream used by this generator.getStream in class RandomVariateGenpublic Distribution getDistribution()
RandomVariateGenDistribution used by this generator.getDistribution in class RandomVariateGenTo submit a bug or ask questions, send an e-mail to Pierre L'Ecuyer.