SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RandomStreamBase.java
1/*
2 * Class: RandomStreamBase
3 * Description: Base class of all random number generators
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 java.io.Serializable;
28
54public abstract class RandomStreamBase implements CloneableRandomStream, Serializable {
55
56 private static final long serialVersionUID = 70510L;
57 // La date de modification a l'envers, lire 10/05/2007
58
59 // constants
60 protected static double invtwo24 = 5.9604644775390625e-8; // 2^(-24)
61 private static double EPSILON = 5.5511151231257827e-17; // 2^(-54)
62
63 protected String name = null;
64
65 // prec53 keeps track if the precision has been increased or not.
66 protected boolean prec53 = false;
67 protected boolean anti = false; // Deprecated.
68
69 public abstract void resetStartStream();
70
71 public abstract void resetStartSubstream();
72
73 public abstract void resetNextSubstream();
74
75 public abstract String toString();
76
95 public void increasedPrecision(boolean incp) {
96 prec53 = incp;
97 }
98
108 protected abstract double nextValue();
109
117 public double nextDouble() {
118 double u = nextValue();
119 if (prec53)
120 u = (u + nextValue() * invtwo24) % 1.0 + EPSILON;
121 if (anti)
122 return 1.0 - u;
123 else
124 return u;
125 }
126
134 public void nextArrayOfDouble(double[] u, int start, int n) {
135 if (u.length == 0)
136 throw new NullPointerException("The array must be initialized.");
137 if (u.length < n + start)
138 throw new IndexOutOfBoundsException("The array is too small.");
139 if (start < 0)
140 throw new IndexOutOfBoundsException("Must start at a " + "non-negative index.");
141 if (n < 0)
142 throw new IllegalArgumentException("Must have a non-negative " + "number of elements.");
143
144 for (int ii = start; ii < start + n; ii++)
145 u[ii] = nextDouble();
146 }
147
157 public int nextInt(int i, int j) {
158 if (i > j)
159 throw new IllegalArgumentException(i + " is larger than " + j + ".");
160 // This works even for an interval [0, 2^31 - 1]. It would not with
161 // return i + (int)(nextDouble() * (j - i + 1));
162 return i + (int) (nextDouble() * (j - i + 1.0));
163 }
164
175 public void nextArrayOfInt(int i, int j, int[] u, int start, int n) {
176 if (u == null)
177 throw new NullPointerException("The array must be " + "initialized.");
178 if (u.length < n + start)
179 throw new IndexOutOfBoundsException("The array is too small.");
180 if (start < 0)
181 throw new IndexOutOfBoundsException("Must start at a " + "non-negative index.");
182 if (n < 0)
183 throw new IllegalArgumentException("Must have a non-negative " + "number of elements.");
184
185 for (int ii = start; ii < start + n; ii++)
186 u[ii] = nextInt(i, j);
187 }
188
192 @Deprecated
193 public String formatState() {
194 return toString();
195 }
196
200 @Deprecated
201 public String formatStateFull() {
202 throw new UnsupportedOperationException(" call the toStringFull() method instead.");
203 }
204
211 RandomStreamBase retour = null;
212 try {
213 retour = (RandomStreamBase) super.clone();
214 } catch (CloneNotSupportedException cnse) {
215 cnse.printStackTrace(System.err);
216 }
217 return retour;
218 }
219
220}
This class provides a convenient foundation on which RNGs can be built.
abstract String toString()
Returns a string containing the current state of this stream.
String formatStateFull()
Use the toStringFull method.
abstract void resetStartSubstream()
Reinitializes the stream to the beginning of its current substream:
double nextDouble()
Returns a uniform random number between 0 and 1 from the stream.
int nextInt(int i, int j)
Calls nextDouble once to create one integer between i and j.
abstract double nextValue()
This method should return the next random number (between 0 and 1) from the current stream.
void nextArrayOfInt(int i, int j, int[] u, int start, int n)
Calls nextInt n times to fill the array u.
RandomStreamBase clone()
Clones the current generator and return its copy.
String formatState()
Use the toString method.
abstract void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
void increasedPrecision(boolean incp)
After calling this method with incp = true, each call to the RNG (direct or indirect) for this stream...
void nextArrayOfDouble(double[] u, int start, int n)
Calls nextDouble n times to fill the array u.
abstract void resetStartStream()
Reinitializes the stream to its initial state : and are set to .
CloneableRandomStream extends RandomStream and Cloneable.