SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RandomVariateGenWithCache.java
1/*
2 * Class: RandomVariateGenWithCache
3 * Description: random variate generator whose values are cached for efficiency
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.randvar;
26
27import umontreal.ssj.randvar.RandomVariateGen;
28import cern.colt.list.DoubleArrayList;
29import umontreal.ssj.rng.RandomStream;
30import umontreal.ssj.probdist.Distribution;
31
51public class RandomVariateGenWithCache extends RandomVariateGen {
52 private RandomVariateGen rvg;
53 private DoubleArrayList values;
54 private int index = 0;
55 private boolean caching = true;
56
64 public RandomVariateGenWithCache(RandomVariateGen rvg) {
65 if (rvg == null)
66 throw new NullPointerException("The given random variate generator cannot be null");
67 this.rvg = rvg;
68 values = new DoubleArrayList();
69 }
70
81 public RandomVariateGenWithCache(RandomVariateGen rvg, int initialCapacity) {
82 if (rvg == null)
83 throw new NullPointerException("The given random variate generator cannot be null");
84 this.rvg = rvg;
85 values = new DoubleArrayList(initialCapacity);
86 }
87
96 public boolean isCaching() {
97 return caching;
98 }
99
106 public void setCaching(boolean caching) {
107 if (this.caching && !caching)
108 clearCache();
109 this.caching = caching;
110 }
111
117 public RandomVariateGen getCachedGen() {
118 return rvg;
119 }
120
128 public void setCachedGen(RandomVariateGen rvg) {
129 if (rvg == null)
130 throw new NullPointerException("The given random variate generator cannot be null");
131 if (rvg == this.rvg)
132 return;
133 this.rvg = rvg;
134 clearCache();
135 }
136
141 public void clearCache() {
142 // values.clear();
143 // Keep the array previously returned by getCachedValues
144 // intact to allow caching values for several
145 // replications.
146 values = new DoubleArrayList();
147 index = 0;
148 }
149
158 public void initCache() {
159 index = 0;
160 }
161
167 public int getNumCachedValues() {
168 return values.size();
169 }
170
180 public int getCacheIndex() {
181 return index;
182 }
183
194 public void setCacheIndex(int newIndex) {
195 if (newIndex < 0 || newIndex > values.size())
196 throw new IllegalArgumentException("newIndex must not be negative or greater than the cache size");
197 index = newIndex;
198 }
199
206 public DoubleArrayList getCachedValues() {
207 return values;
208 }
209
217 public void setCachedValues(DoubleArrayList values) {
218 if (values == null)
219 throw new NullPointerException();
220 this.values = values;
221 index = values.size();
222 }
223
224 public double nextDouble() {
225 if (!caching)
226 return rvg.nextDouble();
227 else if (index >= values.size()) {
228 double v = rvg.nextDouble();
229 values.add(v);
230 ++index;
231 return v;
232 } else
233 return values.getQuick(index++);
234 }
235
236 public void nextArrayOfDouble(double[] v, int start, int n) {
237 if (!caching) {
238 rvg.nextArrayOfDouble(v, start, n);
239 return;
240 }
241 int remainingValues = values.size() - index;
242 if (remainingValues < 0)
243 remainingValues = 0;
244 int ncpy = Math.min(n, remainingValues);
245 if (ncpy > 0) {
246 System.arraycopy(values.elements(), index, v, start, ncpy);
247 index += ncpy;
248 }
249 int ngen = n - ncpy;
250 if (ngen > 0) {
251 rvg.nextArrayOfDouble(v, start + ncpy, ngen);
252 for (int i = ncpy; i < n; i++) {
253 values.add(v[start + i]);
254 ++index;
255 }
256 }
257 }
258
260 return rvg.getStream();
261 }
262
264 return rvg.getDistribution();
265 }
266}
void setCachedGen(RandomVariateGen rvg)
Sets the random variate generator whose values are cached to rvg.
int getCacheIndex()
Return the index of the next cached value that will be returned by the generator.
int getNumCachedValues()
Returns the total number of values cached by this generator.
void setCachedValues(DoubleArrayList values)
Sets the array list containing the cached values to values.
RandomStream getStream()
Returns the umontreal.ssj.rng.RandomStream used by this generator.
RandomVariateGenWithCache(RandomVariateGen rvg, int initialCapacity)
Constructs a new cached random variate generator with internal generator rvg.
void nextArrayOfDouble(double[] v, int start, int n)
Generates n random numbers from the continuous distribution contained in this object.
Distribution getDistribution()
Returns the umontreal.ssj.probdist.Distribution used by this generator.
void initCache()
Resets this generator to recover values from the cache.
RandomVariateGenWithCache(RandomVariateGen rvg)
Constructs a new cached random variate generator with internal generator rvg.
void clearCache()
Clears the cached values for this cached 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.
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
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.
This interface should be implemented by all classes supporting discrete and continuous distributions.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...