libStatGen Software 1
Random Class Reference

Public Member Functions

 Random (long s=0x7654321)
 
int Binary ()
 
double Next ()
 
unsigned long NextInt ()
 
double Normal ()
 
void Reset (long s)
 
void InitMersenne (unsigned long s)
 
 operator double ()
 
double Uniform (double lo=0.0, double hi=1.0)
 
void Choose (int *array, int n, int k)
 
void Choose (int *array, float *weights, int n, int k)
 

Protected Attributes

long seed
 
long last
 
long * shuffler
 
int normSaved
 
double normStore
 
double mersenneMult
 
unsigned long * mt
 
int mti
 

Detailed Description

Definition at line 69 of file Random.h.

Constructor & Destructor Documentation

◆ Random()

Random::Random ( long  s = 0x7654321)

Definition at line 89 of file Random.cpp.

90{
91#ifndef __NO_MERSENNE
92 mt = new unsigned long [MERSENNE_N];
93 mti = MERSENNE_N + 1;
94 mersenneMult = 1.0/4294967296.0;
95#else
96 shuffler = new long [NTAB];
97#endif
98 Reset(s);
99}

◆ ~Random()

Random::~Random ( )

Definition at line 101 of file Random.cpp.

102{
103#ifndef __NO_MERSENNE
104 delete [] mt;
105#else
106 delete [] shuffler;
107#endif
108}

Member Function Documentation

◆ Binary()

int Random::Binary ( )

Definition at line 148 of file Random.cpp.

149{
150 return Next() > 0.5 ? 1 : 0;
151}

◆ Choose() [1/2]

void Random::Choose ( int *  array,
float *  weights,
int  n,
int  k 
)

Definition at line 341 of file Random.cpp.

342{
343 int choices = 1, others = 0;
344
345 if (k > n / 2)
346 {
347 choices = 0;
348 others = 1;
349 k = n - k;
350 }
351
352 // First calculate cumulative sums of weights ...
353 float * cumulative = new float [n + 1];
354
355 cumulative[0] = 0;
356 for (int i = 1; i <= n; i++)
357 cumulative[i] = cumulative[i - 1] + weights[i - 1];
358
359 float & sum = cumulative[n], reject = 0.0;
360
361 for (int i = 0; i < n; i++)
362 array[i] = others;
363
364 while (k > 0)
365 {
366 float weight = Next() * sum;
367
368 int hi = n, lo = 0, i = 0;
369
370 while (hi >= lo)
371 {
372 i = (hi + lo) / 2;
373
374 if (cumulative[i + 1] <= weight)
375 lo = i + 1;
376 else if (cumulative[i] >= weight)
377 hi = i - 1;
378 else break;
379 }
380
381 if (array[i] == choices) continue;
382
383 array[i] = choices;
384 reject += weights[i];
385
386 // After selecting a substantial number of elements, update the cumulative
387 // distribution -- to ensure that at least half of our samples produce a hit
388 if (reject > sum * 0.50)
389 {
390 cumulative[0] = 0;
391 for (int i = 1; i <= n; i++)
392 if (array[i] != choices)
393 cumulative[i] = cumulative[i - 1] + weights[i - 1];
394 else
395 cumulative[i] = cumulative[i - 1];
396
397 reject = 0.0;
398 sum = cumulative[n];
399 }
400
401 k--;
402 }
403
404 delete [] cumulative;
405}

◆ Choose() [2/2]

void Random::Choose ( int *  array,
int  n,
int  k 
)

Definition at line 316 of file Random.cpp.

317{
318 int choices = 1, others = 0;
319
320 if (k > n / 2)
321 {
322 choices = 0;
323 others = 1;
324 k = n - k;
325 }
326
327 for (int i = 0; i < n; i++)
328 array[i] = others;
329
330 while (k > 0)
331 {
332 int i = NextInt() % n;
333
334 if (array[i] == choices) continue;
335
336 array[i] = choices;
337 k--;
338 }
339}

◆ InitMersenne()

void Random::InitMersenne ( unsigned long  s)

Definition at line 133 of file Random.cpp.

134{
135 mt[0]= s & 0xffffffffUL;
136 for (mti = 1; mti < MERSENNE_N; mti++)
137 {
138 mt[mti] = (1812433253UL * (mt[mti-1] ^(mt[mti-1] >> 30)) + mti);
139 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
140 /* In the previous versions, MSBs of the seed affect */
141 /* only MSBs of the array mt[]. */
142 /* 2002/01/09 modified by Makoto Matsumoto */
143
144 mt[mti] &= 0xffffffffUL;
145 }
146}

◆ Next()

double Random::Next ( )

Definition at line 155 of file Random.cpp.

156{
157 unsigned long y;
158
159 // mag01[x] = x * MATRIX_A for x=0,1
160 static unsigned long mag01[2]={0x0UL, MATRIX_A};
161
162 if (mti >= MERSENNE_N)
163 {
164 /* generate MERSENNE_N words at one time */
165 int kk;
166
167 // If InitMersenne() has not been called, a default initial seed is used
168 if (mti == MERSENNE_N+1)
169 InitMersenne(5489UL);
170
171 for (kk=0; kk < MERSENNE_N-MERSENNE_M; kk++)
172 {
173 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
174 mt[kk] = mt[kk+MERSENNE_M] ^(y >> 1) ^ mag01[y & 0x1UL];
175 }
176
177 for (; kk < MERSENNE_N-1; kk++)
178 {
179 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
180 mt[kk] = mt[kk+(MERSENNE_M - MERSENNE_N)] ^(y >> 1) ^ mag01[y & 0x1UL];
181 }
182
183 y = (mt[MERSENNE_N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
184 mt[MERSENNE_N-1] = mt[MERSENNE_M-1] ^(y >> 1) ^ mag01[y & 0x1UL];
185
186 mti = 0;
187 }
188
189 y = mt[mti++];
190
191 // Tempering
192 y ^= (y >> 11);
193 y ^= (y << 7) & 0x9d2c5680UL;
194 y ^= (y << 15) & 0xefc60000UL;
195 y ^= (y >> 18);
196
197 return (mersenneMult *((double) y + 0.5));
198}

◆ NextInt()

unsigned long Random::NextInt ( )

Definition at line 202 of file Random.cpp.

203{
204 unsigned long y;
205
206 // mag01[x] = x * MATRIX_A for x=0,1
207 static unsigned long mag01[2]={0x0UL, MATRIX_A};
208
209 if (mti >= MERSENNE_N)
210 {
211 /* generate MERSENNE_N words at one time */
212 int kk;
213
214 // If InitMersenne() has not been called, a default initial seed is used
215 if (mti == MERSENNE_N + 1)
216 InitMersenne(5489UL);
217
218 for (kk= 0; kk < MERSENNE_N - MERSENNE_M; kk++)
219 {
220 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
221 mt[kk] = mt[kk+MERSENNE_M] ^(y >> 1) ^ mag01[y & 0x1UL];
222 }
223
224 for (; kk< MERSENNE_N-1; kk++)
225 {
226 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
227 mt[kk] = mt[kk+(MERSENNE_M - MERSENNE_N)] ^(y >> 1) ^ mag01[y & 0x1UL];
228 }
229
230 y = (mt[MERSENNE_N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
231 mt[MERSENNE_N-1] = mt[MERSENNE_M-1] ^(y >> 1) ^ mag01[y & 0x1UL];
232
233 mti = 0;
234 }
235
236 y = mt[mti++];
237
238 // Tempering
239 y ^= (y >> 11);
240 y ^= (y << 7) & 0x9d2c5680UL;
241 y ^= (y << 15) & 0xefc60000UL;
242 y ^= (y >> 18);
243
244 return y;
245}

◆ Normal()

double Random::Normal ( )

Definition at line 290 of file Random.cpp.

291{
292 double v1, v2, fac, rsq;
293
294 if (!normSaved) // Do we need new numbers?
295 {
296 do
297 {
298 v1 = 2.0 * Next() - 1.0; // Pick two coordinates from
299 v2 = 2.0 * Next() - 1.0; // -1 to +1 and check if they
300 rsq = v1*v1 + v2*v2; // are in unit circle...
301 }
302 while (rsq >= 1.0 || rsq == 0.0);
303
304 fac = sqrt(-2.0 * log(rsq)/rsq); // Apply the Box-Muller
305 normStore = v1 * fac; // transformation and save
306 normSaved = 1; // one deviate for next time
307 return v2 * fac;
308 }
309 else
310 {
311 normSaved = 0;
312 return normStore;
313 }
314}

◆ operator double()

Random::operator double ( )
inline

Definition at line 114 of file Random.h.

115 {
116 return Next();
117 }

◆ Reset()

void Random::Reset ( long  s)

Definition at line 110 of file Random.cpp.

111{
112 normSaved = 0;
113
114#ifndef __NO_MERSENNE
115 InitMersenne(s);
116#else
117 // 'Continuous' Random Generator
118 if ((seed = s) < 1)
119 seed = s == 0 ? 1 : -s; // seed == 0 would be disastrous
120
121 for (int j=NTAB+7; j>=0; j--) // Warm up and set shuffle table
122 {
123 long k = seed / IQ;
124 seed = IA * (seed - k * IQ) - IR * k;
125 if (seed < 0) seed += IM;
126 if (j < NTAB) shuffler[j] = seed;
127 }
128 last=shuffler[0];
129#endif
130}

◆ Uniform()

double Random::Uniform ( double  lo = 0.0,
double  hi = 1.0 
)
inline

Definition at line 120 of file Random.h.

121 {
122 return lo + (hi - lo) * Next();
123 }

Member Data Documentation

◆ last

long Random::last
protected

Definition at line 77 of file Random.h.

◆ mersenneMult

double Random::mersenneMult
protected

Definition at line 84 of file Random.h.

◆ mt

unsigned long* Random::mt
protected

Definition at line 87 of file Random.h.

◆ mti

int Random::mti
protected

Definition at line 90 of file Random.h.

◆ normSaved

int Random::normSaved
protected

Definition at line 81 of file Random.h.

◆ normStore

double Random::normStore
protected

Definition at line 82 of file Random.h.

◆ seed

long Random::seed
protected

Definition at line 76 of file Random.h.

◆ shuffler

long* Random::shuffler
protected

Definition at line 78 of file Random.h.


The documentation for this class was generated from the following files: