|
SSJ V. 2.6. |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object umontreal.iro.lecuyer.rng.RandomStreamWithCache
public class RandomStreamWithCache
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 Summary | |
---|---|
RandomStreamWithCache(RandomStream stream)
Constructs a new cached random stream with internal stream stream. |
|
RandomStreamWithCache(RandomStream stream,
int initialCapacity)
Constructs a new cached random stream with internal stream stream. |
Method Summary | |
---|---|
void |
clearCache()
Clears the cached values for this random stream. |
RandomStream |
getCachedStream()
Returns a reference to the random stream whose values are cached. |
cern.colt.list.DoubleArrayList |
getCachedValues()
Returns an array list containing the values cached by this random stream. |
int |
getCacheIndex()
Return the index of the next cached value that will be returned by the stream. |
int |
getNumCachedValues()
Returns the total number of values cached by this random stream. |
void |
initCache()
Resets this random stream to recover values from the cache. |
boolean |
isCaching()
Determines if the random stream is caching values, default being true. |
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. |
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. |
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. |
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. |
void |
resetNextSubstream()
Reinitializes the stream to the beginning of its next substream: Ng is computed, and Cg and Bg are set to Ng. |
void |
resetStartStream()
Reinitializes the stream to its initial state Ig: Cg and Bg are set to Ig. |
void |
resetStartSubstream()
Reinitializes the stream to the beginning of its current substream: Cg is set to Bg. |
void |
setCachedStream(RandomStream stream)
Sets the random stream whose values are cached to stream. |
void |
setCachedValues(cern.colt.list.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. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface umontreal.iro.lecuyer.rng.RandomStream |
---|
toString |
Constructor Detail |
---|
public RandomStreamWithCache(RandomStream stream)
stream
- the random stream whose uniforms are cached.
NullPointerException
- if stream is null.public RandomStreamWithCache(RandomStream stream, int initialCapacity)
stream
- the random stream whose values are cached.initialCapacity
- the initial capacity of the cache.
NullPointerException
- if stream is null.Method Detail |
---|
public boolean isCaching()
nextDouble
method simply calls the corresponding method on the internal
random stream, without storing the generated uniforms.
public void setCaching(boolean caching)
clearCache
to clear the cached values.
caching
- the new value of the caching indicator.public RandomStream getCachedStream()
public void setCachedStream(RandomStream stream)
clearCache
method is called to clear the cache.
stream
- the new random stream whose values will be cached.
NullPointerException
- if stream is null.public void clearCache()
public void initCache()
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
.
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.
IllegalArgumentException
- if newIndex
is negative or greater than or equal to the cache size.public cern.colt.list.DoubleArrayList getCachedValues()
public void setCachedValues(cern.colt.list.DoubleArrayList values)
values
- the array list of cached values.
NullPointerException
- if values is null.public void resetStartStream()
RandomStream
resetStartStream
in interface RandomStream
public void resetStartSubstream()
RandomStream
resetStartSubstream
in interface RandomStream
public void resetNextSubstream()
RandomStream
resetNextSubstream
in interface RandomStream
public double nextDouble()
RandomStream
nextDouble
in interface RandomStream
public void nextArrayOfDouble(double[] u, int start, int n)
RandomStream
nextArrayOfDouble
in interface RandomStream
u
- array that will contain the generated uniformsstart
- starting index, in the array u, to write uniforms fromn
- number of uniforms to generatepublic int nextInt(int i, int j)
RandomStream
nextInt
in interface RandomStream
i
- smallest integer that can be generatedj
- greatest integer that can be generated
public void nextArrayOfInt(int i, int j, int[] u, int start, int n)
RandomStream
nextArrayOfInt
in interface RandomStream
i
- smallest integer that can be generatedj
- greatest integer that can be generatedu
- array that will contain the generated valuesstart
- starting index, in the array u, to write integers fromn
- number of values being generated
|
SSJ V. 2.6. |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |