umontreal.iro.lecuyer.randvar
Class RandomVariateGenWithCache
- java.lang.Object
-
- umontreal.iro.lecuyer.randvar.RandomVariateGen
-
- umontreal.iro.lecuyer.randvar.RandomVariateGenWithCache
-
public class RandomVariateGenWithCache extends RandomVariateGen
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 aRandomVariateGeninstance 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 thenextDoublemethod is called after the object is reset (by callingsetCachedValues), 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 Summary
Constructors 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidclearCache()Clears the cached values for this cached generator.RandomVariateGengetCachedGen()Returns a reference to the random variate generator whose values are cached.DoubleArrayListgetCachedValues()Returns an array list containing the values cached by this random variate generator.intgetCacheIndex()Return the index of the next cached value that will be returned by the generator.DistributiongetDistribution()Returns theDistributionused by this generator.intgetNumCachedValues()Returns the total number of values cached by this generator.RandomStreamgetStream()Returns theRandomStreamused by this generator.voidinitCache()Resets this generator to recover values from the cache.booleanisCaching()Determines if the random variate generator is caching values, default being true.voidnextArrayOfDouble(double[] v, int start, int n)Generates n random numbers from the continuous distribution contained in this object.doublenextDouble()Generates a random number from the continuous distribution contained in this object.voidsetCachedGen(RandomVariateGen rvg)Sets the random variate generator whose values are cached to rvg.voidsetCachedValues(DoubleArrayList values)Sets the array list containing the cached values to values.voidsetCacheIndex(int newIndex)Sets the index, in the cache, of the next value returned bynextDouble.voidsetCaching(boolean caching)Sets the caching indicator to caching.-
Methods inherited from class umontreal.iro.lecuyer.randvar.RandomVariateGen
setStream, toString
-
-
-
-
Constructor Detail
-
RandomVariateGenWithCache
public RandomVariateGenWithCache(RandomVariateGen rvg)
Constructs a new cached random variate generator with internal generator rvg.- Parameters:
rvg- the random variate generator whose values are cached.- Throws:
java.lang.NullPointerException- if rvg is null.
-
RandomVariateGenWithCache
public 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:
rvg- the random variate generator whose values are cached.initialCapacity- the number of cached values.- Throws:
java.lang.NullPointerException- if rvg is null.
-
-
Method Detail
-
isCaching
public boolean isCaching()
Determines if the random variate generator is caching values, default being true. When caching is turned OFF, thenextDoublemethod simply calls the corresponding method on the internal random variate generator, without storing the generated values.- Returns:
- the caching indicator.
-
setCaching
public void setCaching(boolean caching)
Sets the caching indicator to caching. If caching is turned OFF, this method callsclearCacheto clear the cached values.- Parameters:
caching- the new value of the caching indicator.
-
getCachedGen
public RandomVariateGen 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.
-
setCachedGen
public void setCachedGen(RandomVariateGen rvg)
Sets the random variate generator whose values are cached to rvg. If the generator is changed, theclearCachemethod is called.- Parameters:
rvg- the new random variate generator whose values are cached.- Throws:
java.lang.NullPointerException- if rvg is null.
-
clearCache
public void clearCache()
Clears the cached values for this cached generator. Any subsequent call will then obtain new values from the internal generator.
-
initCache
public void initCache()
Resets this generator to recover values from the cache. Subsequent calls tonextDoublewill 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 callingsetCacheIndex.
-
getNumCachedValues
public int getNumCachedValues()
Returns the total number of values cached by this generator.- Returns:
- the total number of cached values.
-
getCacheIndex
public int 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 bygetNumCachedValues, and a subsequent call tonextDoublewill 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.
-
setCacheIndex
public void setCacheIndex(int newIndex)
Sets the index, in the cache, of the next value returned bynextDouble. If newIndex is 0, this is equivalent to callinginitCache. If newIndex isgetNumCachedValues, subsequent calls tonextDoublewill add new values to the cache.- Parameters:
newIndex- the new index.- Throws:
java.lang.IllegalArgumentException- if newIndex is negative or greater than or equal to the cache size.
-
getCachedValues
public DoubleArrayList getCachedValues()
Returns an array list containing the values cached by this random variate generator.- Returns:
- the array of cached values.
-
setCachedValues
public 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:
values- the array list of cached values.- Throws:
java.lang.NullPointerException- if values is null.
-
nextDouble
public double nextDouble()
Description copied from class:RandomVariateGenGenerates a random number from the continuous distribution contained in this object. By default, this method uses inversion by calling theinverseFmethod of the distribution object. Alternative generating methods are provided in subclasses.- Overrides:
nextDoublein classRandomVariateGen- Returns:
- the generated value
-
nextArrayOfDouble
public void nextArrayOfDouble(double[] v, int start, int n)Description copied from class:RandomVariateGenGenerates 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 callsnextDouble()n times, but one can override it in subclasses for better efficiency.- Overrides:
nextArrayOfDoublein classRandomVariateGen- Parameters:
v- array in which the variates will be storedstart- starting index, in v, of the new variatesn- number of variates to generate
-
getStream
public RandomStream getStream()
Description copied from class:RandomVariateGenReturns theRandomStreamused by this generator.- Overrides:
getStreamin classRandomVariateGen- Returns:
- the stream associated to this object
-
getDistribution
public Distribution getDistribution()
Description copied from class:RandomVariateGenReturns theDistributionused by this generator.- Overrides:
getDistributionin classRandomVariateGen- Returns:
- the distribution associated to that object
-
-
DMelt 3.0 © DataMelt by jWork.ORG