SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RandomStreamWithCache.java
1/*
2 * Class: RandomStreamWithCache
3 * Description: random stream whose uniforms are cached for more 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.rng;
26
27import cern.colt.list.DoubleArrayList;
28
47public class RandomStreamWithCache implements RandomStream {
48 private RandomStream stream;
49 private DoubleArrayList values;
50 private int index = 0;
51 private boolean caching = true;
52
60 if (stream == null)
61 throw new NullPointerException("The given random stream cannot be null");
62 this.stream = stream;
63 values = new DoubleArrayList();
64 }
65
76 public RandomStreamWithCache(RandomStream stream, int initialCapacity) {
77 if (stream == null)
78 throw new NullPointerException("The given random stream cannot be null");
79 this.stream = stream;
80 values = new DoubleArrayList(initialCapacity);
81 }
82
90 public boolean isCaching() {
91 return caching;
92 }
93
100 public void setCaching(boolean caching) {
101 if (this.caching && !caching)
102 clearCache();
103 this.caching = caching;
104 }
105
112 return stream;
113 }
114
122 public void setCachedStream(RandomStream stream) {
123 if (stream == null)
124 throw new NullPointerException("The given random stream cannot be null");
125 if (stream == this.stream)
126 return;
127 this.stream = stream;
128 clearCache();
129 }
130
135 public void clearCache() {
136 // values.clear();
137 // Keep the array previously returned by getCachedValues
138 // intact to allow caching values for several
139 // replications.
140 values = new DoubleArrayList();
141 index = 0;
142 }
143
151 public void initCache() {
152 index = 0;
153 }
154
160 public int getNumCachedValues() {
161 return values.size();
162 }
163
173 public int getCacheIndex() {
174 return index;
175 }
176
187 public void setCacheIndex(int newIndex) {
188 if (newIndex < 0 || newIndex > values.size())
189 throw new IllegalArgumentException("newIndex must not be negative or greater than the cache size");
190 index = newIndex;
191 }
192
198 public DoubleArrayList getCachedValues() {
199 return values;
200 }
201
209 public void setCachedValues(DoubleArrayList values) {
210 if (values == null)
211 throw new NullPointerException();
212 this.values = values;
213 index = values.size();
214 }
215
216 public void resetStartStream() {
217 stream.resetStartStream();
218 }
219
220 public void resetStartSubstream() {
221 stream.resetStartSubstream();
222 }
223
224 public void resetNextSubstream() {
225 stream.resetNextSubstream();
226 }
227
228 public double nextDouble() {
229 if (!caching)
230 return stream.nextDouble();
231 else if (index >= values.size()) {
232 double v = stream.nextDouble();
233 values.add(v);
234 ++index;
235 return v;
236 } else
237 return values.getQuick(index++);
238 }
239
240 public void nextArrayOfDouble(double[] u, int start, int n) {
241 if (!caching) {
242 stream.nextArrayOfDouble(u, start, n);
243 return;
244 }
245 int remainingValues = values.size() - index;
246 if (remainingValues < 0)
247 remainingValues = 0;
248 int ncpy = Math.min(n, remainingValues);
249 if (ncpy > 0) {
250 System.arraycopy(values.elements(), index, u, start, ncpy);
251 index += ncpy;
252 }
253 int ngen = n - ncpy;
254 if (ngen > 0) {
255 stream.nextArrayOfDouble(u, start + ncpy, ngen);
256 for (int i = ncpy; i < n; i++) {
257 values.add(u[start + i]);
258 ++index;
259 }
260 }
261 }
262
263 public int nextInt(int i, int j) {
264 return i + (int) (nextDouble() * (j - i + 1));
265 }
266
267 public void nextArrayOfInt(int i, int j, int[] u, int start, int n) {
268 for (int x = start; x < start + n; x++)
269 u[x] = nextInt(i, j);
270 }
271}
int nextInt(int i, int j)
Returns a (pseudo)random number from the discrete uniform distribution over the integers ,...
void nextArrayOfDouble(double[] u, int start, int n)
Generates n (pseudo)random numbers from the uniform distribution and stores them into the array u sta...
boolean isCaching()
Determines if the random stream is caching values, default being true.
RandomStreamWithCache(RandomStream stream)
Constructs a new cached random stream with internal stream stream.
void setCachedValues(DoubleArrayList values)
Sets the array list containing the cached values to values.
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
int getNumCachedValues()
Returns the total number of values cached by this random stream.
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 ,...
void resetStartSubstream()
Reinitializes the stream to the beginning of its current substream:
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...
void setCachedStream(RandomStream stream)
Sets the random stream whose values are cached to stream.
void initCache()
Resets this random stream to recover values from the cache.
void setCaching(boolean caching)
Sets the caching indicator to caching.
void setCacheIndex(int newIndex)
Sets the index, in the cache, of the next value returned by nextDouble.
void resetStartStream()
Reinitializes the stream to its initial state : and are set to .
RandomStreamWithCache(RandomStream stream, int initialCapacity)
Constructs a new cached random stream with internal stream stream.
int getCacheIndex()
Return the index of the next cached value that will be returned by the stream.
RandomStream getCachedStream()
Returns a reference to the random stream whose values are cached.
void clearCache()
Clears the cached values for this random stream.
DoubleArrayList getCachedValues()
Returns an array list containing the values cached by this random stream.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...