SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
WELL512.java
1/*
2 * Class: WELL512
3 * Description: a Well Equidistributed Long period Linear Random Number
4 Generator with a state size of 512 bits
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.rng;
27
28import java.io.Serializable;
29
45public class WELL512 extends RandomStreamBase {
46
47 private static final long serialVersionUID = 120307L;
48 // La date de modification a l'envers, lire 07/03/2012
49
50 private static final double NORM = (1.0 / 0x100000001L);
51
52 private static final int W = 32;
53 private static final int R = 16;
54 private static final int P = 0;
55 private static final int M1 = 13;
56 private static final int M2 = 9;
57 private static final int M3 = 5;
58 private static final int MASK = 0xF; // = 15
59
60 // state variables
61 private int state_i;
62 private int[] state;
63
64 // stream and substream variables :
65 private int[] stream;
66 private int[] substream;
67 private static int[] curr_stream = new int[] { 0xA341BF9A, 0xAFE4901B, 0x6B10DE18, 0x05FE1420, 0xE48B1A9C,
68 0x590AE15E, 0xC5EB82A7, 0x37EAB2F9, 0x90E1C6EA, 0x3AE63902, 0x735DC91C, 0x902E3A8C, 0x6CB28A5D, 0x8474E7D1,
69 0x843E01A3, 0x5A7370EF };
70
71 // P(z) = {0xa7600001, 0xe0f4f3e2, 0xcb30e185, 0x7d6b79a9,
72 // 0xf3d46237, 0x13a524cb, 0x38e3c2d2, 0xa1381bcb,
73 // 0xf7ab5f06, 0x04a72cda, 0x4e302521, 0xaca072f1,
74 // 0x4dd96181, 0x24aa25c9, 0x3c417e7, 0x0, 0x1}
75
76 // Ce tableau represente les 512 coefficients du polynome suivant
77 // (z^(2^200) mod P(z)) mod 2
78 // P(z) est le polynome caracteristique du generateur.
79 private static final int[] pw = new int[] { 0x280009a9, 0x31e221d0, 0xa00c0296, 0x763d492b, 0x63875b75, 0xef2acc3a,
80 0x1400839f, 0x5e0c8526, 0x514e11b, 0x56b398e4, 0x9436c8b9, 0xa6d8130b, 0xc0a48a78, 0x26ad57d0, 0xa3a0c62a,
81 0x3ff16c9b };
82
83 // Ce tableau represente les 512 coefficients du polynome suivant
84 // (z^(2^350) mod P(z)) mod 2
85 // P(z) est le polynome caracteristique du generateur.
86 private static final int[] pz = new int[] { 0xcd68f2fe, 0x183e969a, 0x760449ae, 0xaa0ce54e, 0xfb5363af, 0x79deea9b,
87 0xef66c516, 0x103543cb, 0x244d1a97, 0x7570bc91, 0x31203fc7, 0x455ea2ca, 0xd77d327d, 0xd8c6a83c, 0xc51b05e7,
88 0x300c1501 };
89
90 private void advanceSeed(int[] seed, int[] p) {
91 int b;
92 int[] x = new int[R];
93
94 for (int i = 0; i < R; i++) {
95 state[i] = seed[i];
96 }
97 state_i = 0;
98
99 for (int j = 0; j < R; ++j) {
100 b = p[j];
101 for (int k = 0; k < W; ++k) {
102 if ((b & 1) == 1) {
103 for (int i = 0; i < R; i++) {
104 x[i] ^= state[(state_i + i) & MASK];
105 }
106 }
107 b >>= 1;
108
109 nextValue();
110 }
111 }
112
113 for (int i = 0; i < R; i++) {
114 seed[i] = x[i];
115 }
116 }
117
118 private static void verifySeed(int[] seed) {
119 if (seed.length < R)
120 throw new IllegalArgumentException("Seed must contain " + R + "values");
121 for (int i = 0; i < R; i++)
122 if (seed[i] != 0)
123 return;
124 throw new IllegalArgumentException("At least one of the element of the seed must not be 0.");
125 }
126
127 private WELL512(int i) {
128 // unit vector (to build the state transition matrice)
129 state = new int[R];
130 for (int j = 0; j < R; j++)
131 state[j] = 0;
132 state[i / W] = 1 << (i % W);
133 state_i = 0;
134 }
135
139 public WELL512() {
140 state = new int[R];
141 stream = new int[R];
142 substream = new int[R];
143
144 for (int i = 0; i < R; i++)
145 stream[i] = curr_stream[i];
146
147 advanceSeed(curr_stream, pz);
149 }
150
157 public WELL512(String name) {
158 this();
159 this.name = name;
160 }
161
169 public static void setPackageSeed(int seed[]) {
170 verifySeed(seed);
171 for (int i = 0; i < R; i++)
172 curr_stream[i] = seed[i];
173 }
174
187 public void setSeed(int seed[]) {
188 verifySeed(seed);
189 for (int i = 0; i < R; i++)
190 stream[i] = seed[i];
192 }
193
200 public int[] getState() {
201 int[] result = new int[R];
202 for (int i = 0; i < R; i++)
203 result[i] = state[(state_i + i) & MASK];
204 return result;
205 }
206
212 public WELL512 clone() {
213 WELL512 retour = null;
214
215 retour = (WELL512) super.clone();
216 retour.state = new int[R];
217 retour.substream = new int[R];
218 retour.stream = new int[R];
219
220 for (int i = 0; i < R; i++) {
221 retour.state[i] = state[i];
222 retour.substream[i] = substream[i];
223 retour.stream[i] = stream[i];
224 }
225 return retour;
226 }
227
228 public void resetStartStream() {
229 for (int i = 0; i < R; i++)
230 substream[i] = stream[i];
232 }
233
234 public void resetStartSubstream() {
235 state_i = 0;
236 for (int i = 0; i < R; i++)
237 state[i] = substream[i];
238 }
239
240 public void resetNextSubstream() {
241 advanceSeed(substream, pw);
243 }
244
245 public String toString() {
246 StringBuffer sb = new StringBuffer();
247
248 if (name == null)
249 sb.append("The state of this WELL512 is : {");
250 else
251 sb.append("The state of " + name + " is : {");
252 for (int i = 0; i < R - 1; i++)
253 sb.append(state[(state_i + i) & MASK] + ", ");
254 sb.append(state[(state_i + R - 1) & MASK] + "}");
255
256 return sb.toString();
257 }
258
259 protected double nextValue() {
260 int z0, z1, z2;
261 z0 = state[(state_i + 15) & MASK];
262 z1 = (state[state_i] ^ (state[state_i] << 16))
263 ^ (state[(state_i + M1) & MASK] ^ (state[(state_i + M1) & MASK] << 15));
264 z2 = (state[(state_i + M2) & MASK] ^ (state[(state_i + M2) & MASK] >>> 11));
265 state[state_i] = z1 ^ z2;
266 state[(state_i + 15) & MASK] = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28)
267 ^ (state[state_i] ^ ((state[state_i] << 5) & 0xDA442D24));
268 state_i = (state_i + 15) & MASK;
269
270 long result = state[state_i];
271
272 return (double) (result > 0 ? result : (result + 0x100000000L)) * NORM;
273 }
274
275}
This class provides a convenient foundation on which RNGs can be built.
int[] getState()
Returns the current state of the stream, represented as an array of 16 integers.
Definition WELL512.java:200
WELL512(String name)
Constructs a new stream with the identifier name (used in the toString method).
Definition WELL512.java:157
void resetStartStream()
Reinitializes the stream to its initial state : and are set to .
Definition WELL512.java:228
double nextValue()
This method should return the next random number (between 0 and 1) from the current stream.
Definition WELL512.java:259
static void setPackageSeed(int seed[])
Sets the initial seed of the class WELL512 to the 16 integers of the vector seed[0....
Definition WELL512.java:169
WELL512()
Constructs a new stream.
Definition WELL512.java:139
void setSeed(int seed[])
This method is discouraged for normal use.
Definition WELL512.java:187
void resetStartSubstream()
Reinitializes the stream to the beginning of its current substream:
Definition WELL512.java:234
String toString()
Returns a string containing the current state of this stream.
Definition WELL512.java:245
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
Definition WELL512.java:240
WELL512 clone()
Clones the current generator and return its copy.
Definition WELL512.java:212