Debian Codegraph @

src/ITGSend/newran/newran1.cpp

raw symbols SCIP
1 /// \ingroup newran
2 ///@{
3
4 /// \file newran1.cpp
5 /// Uniform random number generators.
6
7 //#define WANT_STREAM
8 #define WANT_MATH
9 #define WANT_STRING
10 #define WANT_FSTREAM
11
12 #include "include.h"
13
14 #include "newran.h"
15
16 #ifdef use_namespace
17 namespace NEWRAN { using namespace RBD_COMMON; }
18 namespace RBD_LIBRARIES { using namespace NEWRAN; }
19 namespace NEWRAN {
20 #endif
21
22 static char hex(unsigned long s, int n)
23 {
24 unsigned long sn = (s >> n) & 0xF;
25 if (sn < 10) return (char)('0' + sn);
26 else return (char)('A' + (sn - 10));
27 }
28
29 static unsigned long unhex(char c, int n)
30 {
31 if (c < '0') Throw(Runtime_error("newran: invalid character in unhex"));
32 if (c <= '9') return ((unsigned long)(c - '0')) << n;
33 if (c < 'A') Throw(Runtime_error("newran: invalid character in unhex"));
34 if (c <= 'F') return ((unsigned long)(c - 'A' + 10)) << n;
35 Throw(Runtime_error("newran: invalid character in unhex"));
36 #ifndef UseExceptions
37 return 0;
38 #endif
39 }
40
41
42
43 //***************************** Base RNG *********************************
44
45 const char* Random::Name() { return "Random"; }
46
47 Random* Random::RNG = new DummyRNG; // so we get an error message if accessed
48 Random* Random::Dummy = RNG; // so we can get back DummyRNG back
49 SimpleString Random::Dir; // directory where seed is stored
50
51 Real Random::Density(Real) const
52 {
53 Throw(Logic_error("Newran: density function not available"));
54 #ifndef UseExceptions
55 return 0;
56 #endif
57 }
58
59 unsigned long Random::ulNext()
60 {
61 Throw(Logic_error("Newran: ulNext function not available"));
62 #ifndef UseExceptions
63 return 0;
64 #endif
65 }
66
67 void Random::Set(Random& r) // set urng type
68 {
69 RNG = &r;
70 }
71
72 void Random::CopySeedFromDisk(bool update)
73 {
74 if (Dir.size() == 0) Throw(Logic_error("Newran: directory not defined"));
75 RNG->CSFD(update);
76 }
77
78 void Random::CopySeedToDisk()
79 {
80 if (Dir.size() == 0) Throw(Logic_error("Newran: directory not defined"));
81 RNG->CSTD();
82 }
83
84 void Random::SetDirectory(const char* dir)
85 {
86 Dir = dir;
87 }
88
89 //***************** verify we are dealing with a seed file *********************
90
91 static void ReadVerify(ifstream& in, bool missing_ok = false)
92 {
93 if (!in || in.eof())
94 {
95 if (missing_ok) return;
96 Throw(Runtime_error("Newran: can't open seed file"));
97 }
98 const char* v_string = "!Newran03!"; char c;
99 for (int i = 0; i < 10; ++i)
100 {
101 in >> c;
102 if (in.eof())
103 {
104 if (i==0 && missing_ok) return;
105 Throw(Runtime_error("Newran: seed file verification fails"));
106 }
107 if (c != v_string[i])
108 Throw(Runtime_error("Newran: seed file verification fails"));
109 }
110 }
111
112 static void WriteVerify(ofstream& out)
113 {
114 if (!out) Throw(Runtime_error("Newran: can't access seed file"));
115 const char* v_string = "!Newran03!";
116 for (int i = 0; i < 10; ++i) out << v_string[i];
117 }
118
119
120 //*********************** base of LGM Generators ******************************
121
122 const char* LGM_base::Name() { return "LGM_base"; }
123
124 LGM_base::LGM_base(double s)
125 {
126 if (s>=1.0 || s<=0.0)
127 Throw(Logic_error("Newran: seed out of range"));
128 seed = (long)(s*2147483648UL);
129 Update = false;
130 }
131
132 void LGM_base::NextValue()
133 {
134 // m = 2147483647 = 2^31 - 1; a = 16807;
135 // 127773 = m div a; 2836 = m mod a
136 long hi = seed / 127773L; // integer division
137 long lo = seed - hi * 127773L; // modulo
138 seed = 16807 * lo - 2836 * hi;
139 if (seed <= 0) seed += 2147483647L;
140 }
141
142 Real LGM_base::Next() { NextValue(); return (double)seed / 2147483648.0; }
143
144 unsigned long LGM_base::ulNext() { NextValue(); return (unsigned long)seed << 1; }
145
146
147 //*********************** mixed LGM Generator ******************************
148
149 const char* LGM_mixed::Name() { return "LGM_mixed"; }
150
151 LGM_mixed::LGM_mixed(double s) : LGM_base(s)
152 {
153 for (int i = 0; i<128; i++) { LGM_base::NextValue(); Buffer[i] = seed; }
154 }
155
156 void LGM_mixed::NextValue() // get new mixed random number
157 {
158 int i = (int)(LGM_base::Next()*128); // 0 <= i < 128
159 LGM_base::NextValue();
160 seed_mixed = Buffer[i]; Buffer[i] = seed;
161 }
162
163 Real LGM_mixed::Next() { NextValue(); return (double)seed_mixed / 2147483648.0; }
164
165 unsigned long LGM_mixed::ulNext()
166 { NextValue(); return (unsigned long)seed_mixed << 1; }
167
168
169 void LGM_mixed::CSFD(bool update)
170 {
171 Update = false;
172 ifstream in((Dir+"lgm_mix.txt").c_str());
173 ReadVerify(in);
174 char x; seed = 0;
175 for (int n = 28; n >= 0; n -= 4) { in >> x; seed |= unhex(x, n); }
176 ReadVerify(in);
177 in.close();
178 for (int i = 0; i<128; i++) { LGM_base::NextValue(); Buffer[i] = seed; }
179 Update = update;
180 }
181
182 void LGM_mixed::CSTD()
183 {
184 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
185 ReadVerify(in, true); if (in) in.close();
186 ofstream out((Dir+"lgm_mix.txt").c_str());
187 WriteVerify(out);
188 for (int n = 28; n >= 0; n -= 4) out << hex(seed, n);
189 WriteVerify(out);
190 out << endl;
191 out.close();
192 }
193
194
195 LGM_mixed::~LGM_mixed()
196 {
197 if (Update) CSTD();
198 if (RNG == this) RNG = Dummy;
199 }
200
201 //*********************** simple LGM Generator ******************************
202
203 const char* LGM_simple::Name() { return "LGM_simple"; }
204
205 LGM_simple::LGM_simple(double s) : LGM_base(s) {}
206
207 void LGM_simple::CSFD(bool update)
208 {
209 Update = false;
210 ifstream in((Dir+"lgm.txt").c_str());
211 ReadVerify(in);
212 seed = 0;
213 char x;
214 for (int n = 28; n >= 0; n -= 4) { in >> x; seed |= unhex(x, n); }
215 ReadVerify(in);
216 in.close();
217 Update = update;
218 }
219
220 void LGM_simple::CSTD()
221 {
222 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
223 ReadVerify(in, true); if (in) in.close();
224 ofstream out((Dir+"lgm.txt").c_str());
225 WriteVerify(out);
226 for (int n = 28; n >= 0; n -= 4) out << hex(seed, n);
227 WriteVerify(out);
228 out << endl;
229 out.close();
230 }
231
232
233 LGM_simple::~LGM_simple()
234 {
235 if (Update) CSTD();
236 if (RNG == this) RNG = Dummy;
237 }
238
239
240 //*********************** Wichmann Hill Generator ******************************
241
242 //B.A. Wichmann and I. D. Hill (1982). Algorithm AS 183: An Efficient and Portable
243 //Pseudo-random Number Generator, Applied Statistics, 31, 188-190;
244 //Remarks: 34,p.198 and 35, p.89.
245
246
247 const char* WH::Name() { return "Wichmann Hill"; }
248
249 WH::WH(double s)
250 {
251 if (s>=1.0 || s<=0.0)
252 Throw(Logic_error("Newran: seed out of range"));
253 seed1 = (unsigned long)(s*30269);
254 seed2 = 1234;
255 seed3 = 5678;
256 Update = false;
257 }
258
259 Real WH::Next() // get new uniform random number
260 {
261 seed1 = 171 * seed1 % 30269;
262 seed2 = 172 * seed2 % 30307;
263 seed3 = 170 * seed3 % 30323;
264 double x = seed1 / 30269.0 + seed2 / 30307.0 + seed3 / 30323.0;
265 if (x < 1) return x;
266 else if (x < 2) return x - 1;
267 else return x - 2;
268 }
269
270 unsigned long WH::ulNext() { return (unsigned long)floor(WH::Next() * 4294967296.0); }
271
272 void WH::CSFD(bool update)
273 {
274 Update = false;
275 ifstream in((Dir+"wh.txt").c_str());
276 ReadVerify(in);
277 seed1 = seed2 = seed3 = 0;
278 char x; int n;
279 for (n = 28; n >= 0; n -= 4) { in >> x; seed1 |= unhex(x, n); }
280 for (n = 28; n >= 0; n -= 4) { in >> x; seed2 |= unhex(x, n); }
281 for (n = 28; n >= 0; n -= 4) { in >> x; seed3 |= unhex(x, n); }
282 ReadVerify(in);
283 in.close();
284 Update = update;
285 }
286
287 void WH::CSTD()
288 {
289 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
290 ReadVerify(in, true); if (in) in.close();
291 ofstream out((Dir+"wh.txt").c_str());
292 WriteVerify(out);
293 int n;
294 for (n = 28; n >= 0; n -= 4) out << hex(seed1, n);
295 for (n = 28; n >= 0; n -= 4) out << hex(seed2, n);
296 for (n = 28; n >= 0; n -= 4) out << hex(seed3, n);
297 WriteVerify(out);
298 out << endl;
299 out.close();
300 }
301
302
303 WH::~WH()
304 {
305 if (Update) CSTD();
306 if (RNG == this) RNG = Dummy;
307 }
308
309
310
311
312 //***************** The Mother of all random number generators **********
313
314 const char* MotherOfAll::Name() { return "MotherOfAll"; }
315
316
317
318 MotherOfAll::MotherOfAll(double s) : mStart(1)
319 {
320 // updated 27 September 2005
321 if (s>=1.0 || s<=0.0)
322 Throw(Logic_error("Newran: seed out of range"));
323 seed = (unsigned long)(s*2147483648.0);
324 // check again to make sure it hasn't rounded to an invalid value
325 if (seed>2147483647 || seed==0)
326 Throw(Logic_error("Newran: seed out of range"));
327 Update = false;
328 }
329
330
331 #define m16Long 65536L /* 2^16 */
332 #define m16Mask 0xFFFF /* mask for lower 16 bits */
333 #define m15Mask 0x7FFF /* mask for lower 15 bits */
334 #define m31Mask 0x7FFFFFFF /* mask for 31 bits */
335 #define m32Double 4294967295.0 /* 2^32-1 */
336
337
338
339 // George Marsaglia's The mother of all random number generators
340 // producing uniformly distributed pseudo random 32 bit values with
341 // period about 2^250.
342 // The text of Marsaglia's posting is appended at the end of the function.
343 //
344 // The arrays mother1 and mother2 store carry values in their
345 // first element, and random 16 bit numbers in elements 1 to 8.
346 // These random numbers are moved to elements 2 to 9 and a new
347 // carry and number are generated and placed in elements 0 and 1.
348 // The arrays mother1 and mother2 are filled with random 16 bit values
349 // on first call of Mother by another generator. mStart is the switch.
350 //
351 // Returns:
352 // A 32 bit random number is obtained by combining the output of the
353 // two generators and returned in *pSeed. It is also scaled by
354 // 2^32-1 and returned as a double between 0 and 1
355 //
356 // SEED:
357 // The initial value of *pSeed may be any long value
358 //
359 // Bob Wheeler 8/8/94 <bwheeler (at) ssnet.com>
360 //
361
362
363 void MotherOfAll::Mother()
364 {
365 unsigned long number, number1, number2;
366 short n, *p;
367 unsigned short sNumber;
368
369 // Initialize motheri with 9 random values the first time
370 if (mStart)
371 {
372 sNumber = (unsigned short)(seed & m16Mask); // The low 16 bits
373 number = seed & m31Mask; // Only want 31 bits
374
375 p = mother1;
376 for (n = 18; n--;)
377 {
378 number = 30903*sNumber + (number>>16); // One line multiply-with-carry
379 *p++ = sNumber = (unsigned short)(number & m16Mask);
380 if (n==9) p = mother2;
381 }
382 // make cary 15 bits
383 mother1[0] &= m15Mask; mother2[0] &= m15Mask; mStart = 0;
384 }
385
386 // Move elements 1 to 8 to 2 to 9
387 memmove(mother1+2, mother1+1, 8*sizeof(short));
388 memmove(mother2+2, mother2+1, 8*sizeof(short));
389
390 // Put the carry values in numberi
391 number1 = mother1[0]; number2 = mother2[0];
392
393 // Form the linear combinations
394 number1 += 1941*mother1[2]+1860*mother1[3]+1812*mother1[4]+1776*mother1[5]+
395 1492*mother1[6]+1215*mother1[7]+1066*mother1[8]+12013*mother1[9];
396 number2 += 1111*mother2[2]+2222*mother2[3]+3333*mother2[4]+4444*mother2[5]+
397 5555*mother2[6]+6666*mother2[7]+7777*mother2[8]+9272*mother2[9];
398
399 // Save the high bits of numberi as the new carry
400 mother1[0] = (short)(number1 / m16Long);
401 mother2[0] = (short)(number2 / m16Long);
402 // Put the low bits of numberi into motheri[1]
403 mother1[1] = (short)(m16Mask & number1);
404 mother2[1] = (short)(m16Mask & number2);
405
406 // Combine the two 16 bit random numbers into one 32 bit
407 seed = (((long)mother1[1]) << 16) + (long)mother2[1];
408 seed &= 0xFFFFFFFF;
409
410 }
411
412 Real MotherOfAll::Next() { Mother(); return ((double)seed + 0.5) / 4294967296.0; }
413
414 unsigned long MotherOfAll::ulNext() { Mother(); return seed; }
415
416
417 void MotherOfAll::CSFD(bool update)
418 {
419 Update = false;
420 ifstream in((Dir+"mother.txt").c_str());
421 ReadVerify(in);
422 char x;
423 for (int i = 0; i < 10; ++i)
424 {
425 int n; unsigned long m1 = 0, m2 = 0;
426 for (n = 12; n >= 0; n -= 4) { in >> x; m1 |= unhex(x, n); }
427 for (n = 12; n >= 0; n -= 4) { in >> x; m2 |= unhex(x, n); }
428 mother1[i] = (short)m1;
429 mother2[i] = (short)m2;
430 }
431 ReadVerify(in);
432 in.close();
433 mStart = 0;
434 Update = update;
435 }
436
437 void MotherOfAll::CSTD()
438 {
439 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
440 ReadVerify(in, true); if (in) in.close();
441 ofstream out((Dir+"mother.txt").c_str());
442 WriteVerify(out);
443 for (int i = 0; i < 10; ++i)
444 {
445 int n; unsigned long m1 = mother1[i], m2 = mother2[i];
446 for (n = 12; n >= 0; n -= 4) out << hex(m1, n);
447 for (n = 12; n >= 0; n -= 4) out << hex(m2, n);
448 }
449 WriteVerify(out);
450 out << endl;
451 out.close();
452 }
453
454
455 MotherOfAll::~MotherOfAll()
456 {
457 if (Update) CSTD();
458 if (RNG == this) RNG = Dummy;
459 }
460
461 #undef m16Long
462 #undef m16Mask
463 #undef m15Mask
464 #undef m31Mask
465 #undef m32Double
466
467
468
469
470 //*************** Multiply with carry generator *************************
471
472 const char* MultWithCarry::Name() { return "MultWithCarry"; }
473
474 MultWithCarry::MultWithCarry(double s)
475 {
476 if (s>=1.0 || s<=0.0)
477 Throw(Logic_error("Newran: seed out of range"));
478 x = (unsigned long)(s * 4294967296.0);
479 crry = 1234567;
480 Update = false;
481 }
482
483
484 #ifdef HAS_INT64
485
486 void MultWithCarry::NextValue()
487 {
488 unsigned _int64 axc = (unsigned _int64)x * 2083801278 + crry;
489 crry = (unsigned long)(axc >> 32);
490 x = (unsigned long)axc;
491 }
492
493 #else
494
495 // carry out 32bit * 32bit multiply in software
496
497 void MultWithCarry::NextValue()
498 {
499 unsigned long mult = 2083801278;
500 unsigned long m_hi = mult >> 16;
501 unsigned long m_lo = mult & 0xFFFF;
502
503 unsigned long x_hi = x >> 16;
504 unsigned long x_lo = x & 0xFFFF;
505
506 unsigned long c_hi = crry >> 16;
507 unsigned long c_lo = crry & 0xFFFF;
508
509 x = x_lo * m_lo + c_lo;
510 unsigned long axc = x_lo * m_hi + x_hi * m_lo + c_hi + (x >> 16);
511 crry = x_hi * m_hi + (axc >> 16);
512
513 x = (x & 0xFFFF) + ((axc << 16) & 0xFFFFFFFF);
514
515 }
516
517 #endif
518
519
520 Real MultWithCarry::Next() { NextValue(); return ((double)x + 0.5) / 4294967296.0; }
521
522 unsigned long MultWithCarry::ulNext() { NextValue(); return x; }
523
524
525
526 void MultWithCarry::CSFD(bool update)
527 {
528 Update = false;
529 ifstream in((Dir+"multwc.txt").c_str());
530 ReadVerify(in);
531 x = 0; crry = 0;
532 char xx;
533 int n;
534 for (n = 28; n >= 0; n -= 4) { in >> xx; x |= unhex(xx, n); }
535 for (n = 28; n >= 0; n -= 4) { in >> xx; crry |= unhex(xx, n); }
536 ReadVerify(in);
537 in.close();
538 Update = update;
539 }
540
541 void MultWithCarry::CSTD()
542 {
543 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
544 ReadVerify(in, true); if (in) in.close();
545 ofstream out((Dir+"multwc.txt").c_str());
546 WriteVerify(out);
547 int n;
548 for (n = 28; n >= 0; n -= 4) out << hex(x, n);
549 for (n = 28; n >= 0; n -= 4) out << hex(crry, n);
550 WriteVerify(out);
551 out << endl;
552 out.close();
553 }
554
555 MultWithCarry::~MultWithCarry()
556 {
557 if (Update) CSTD();
558 if (RNG == this) RNG = Dummy;
559 }
560
561
562 //***************************** MT generator ****************************
563
564 /*
565 A C-program for MT19937, with initialization improved 2002/1/26.
566 Coded by Takuji Nishimura and Makoto Matsumoto.
567
568 Before using, initialize the state by using init_genrand(seed)
569 or init_by_array(init_key, key_length).
570
571 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
572 All rights reserved.
573
574 Redistribution and use in source and binary forms, with or without
575 modification, are permitted provided that the following conditions
576 are met:
577
578 1. Redistributions of source code must retain the above copyright
579 notice, this list of conditions and the following disclaimer.
580
581 2. Redistributions in binary form must reproduce the above copyright
582 notice, this list of conditions and the following disclaimer in the
583 documentation and/or other materials provided with the distribution.
584
585 3. The names of its contributors may not be used to endorse or promote
586 products derived from this software without specific prior written
587 permission.
588
589 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
590 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
591 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
592 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
593 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
594 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
595 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
596 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
597 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
598 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
599 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
600
601
602 Any feedback is very welcome.
603 http://www.math.keio.ac.jp/matumoto/emt.html
604 email: matumoto (at) math.keio.ac.jp
605 */
606
607
608 // Period parameters
609 #define N 624
610 #define M 397
611 #define MATRIX_A 0x9908b0dfUL // constant vector a
612 #define UPPER_MASK 0x80000000UL // most significant w-r bits
613 #define LOWER_MASK 0x7fffffffUL // least significant r bits
614
615 static unsigned long mt[N]; // the array for the state vector
616 static int mti=N+1; // mti==N+1 means mt[N] is not initialized
617
618 // initializes mt[N] with a seed
619 void MT::init_genrand(unsigned long s)
620 {
621 mt[0]= s & 0xffffffffUL;
622 for (mti=1; mti<N; mti++)
623 {
624 mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
625 // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
626 // In the previous versions, MSBs of the seed affect
627 // only MSBs of the array mt[].
628 // 2002/01/09 modified by Makoto Matsumoto
629 mt[mti] &= 0xffffffffUL;
630 // for >32 bit machines
631 }
632 }
633
634 // generates a random number on [0,0xffffffff]-interval
635
636 unsigned long MT::genrand_int32()
637 {
638 unsigned long y;
639 static unsigned long mag01[2]={0x0UL, MATRIX_A};
640 // mag01[x] = x * MATRIX_A for x=0,1
641
642 if (mti >= N) // generate N words at one time
643 {
644 int kk;
645
646 if (mti == N+1) // if init_genrand() has not been called,
647 init_genrand(5489UL); // a default initial seed is used
648
649 for (kk=0;kk<N-M;kk++)
650 {
651 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
652 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
653 }
654
655 for (;kk<N-1;kk++)
656 {
657 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
658 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
659 }
660
661 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
662 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
663
664 mti = 0;
665 }
666
667 y = mt[mti++];
668
669 // Tempering
670 y ^= (y >> 11);
671 y ^= (y << 7) & 0x9d2c5680UL;
672 y ^= (y << 15) & 0xefc60000UL;
673 y ^= (y >> 18);
674
675 return y;
676 }
677
678
679 void MT::CSFD(bool update)
680 {
681 Update = false;
682 ifstream in((Dir+"mt19937.txt").c_str());
683 ReadVerify(in);
684
685 char xx;
686 for (int i = 0; i < N; ++i)
687 {
688 unsigned long x = 0;
689 for (int n = 28; n >= 0; n -= 4) { in >> xx; x |= unhex(xx, n); }
690 mt[i] = x;
691 }
692 mti = 0;
693 for (int n = 28; n >= 0; n -= 4) { in >> xx; mti |= unhex(xx, n); }
694 ReadVerify(in);
695 in.close();
696 Update = update;
697 }
698
699 void MT::CSTD()
700 {
701 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
702 ReadVerify(in, true); if (in) in.close();
703 ofstream out((Dir+"mt19937.txt").c_str());
704 WriteVerify(out);
705 for (int i = 0; i < N; ++i)
706 {
707 unsigned long x = mt[i];
708 for (int n = 28; n >= 0; n -= 4) out << hex(x, n);
709 }
710 for (int n = 28; n >= 0; n -= 4) out << hex(mti, n);
711
712 WriteVerify(out);
713 out << endl;
714 out.close();
715 }
716
717 MT::MT(double s)
718 {
719 init_genrand((unsigned long)(s*4294967296.0));
720 Update = false;
721 }
722
723
724 Real MT::Next()
725 {
726 return ((double)genrand_int32() + 0.5) / 4294967296.0;
727 // divided by 2^32
728 }
729
730
731 const char* MT::Name() { return "MT19937"; }
732
733 MT::~MT()
734 {
735 if (Update) CSTD();
736 if (RNG == this) RNG = Dummy;
737 }
738
739 #undef N
740 #undef M
741 #undef MATRIX_A
742 #undef UPPER_MASK
743 #undef LOWER_MASK
744
745
746 //*************************** Fishman Moore generator *********************
747
748 //References
749 //G.S. Fishman and L.R. Moore (1986), An exhaustive analysis of multiplicative
750 // congruential random number generators with modulus 2^31-1, SIAM J Sci.
751 // Stat. Comput., 7, pp. 24-45.
752
753 //Based on code supplied by Tony Cooper (written by him?) */
754
755 #define D31M1 2147483647L /* Mersenne prime 2^31-1 */
756 #define a 950706376.0 /* congruential multiplier = 0x38AAA0C8 */
757 #define a1 0x000038AAL /* 14506 = a >> 16 */
758 #define a0 0x0000A0C8L /* 41160 = a & 0x0000FFFF */
759 #define a1ma0 0xFFFF97E2L /* -26654 = (a1 minus a0) */
760
761 void FM::NextValue()
762 {
763 unsigned long x = seed;
764 unsigned long a1x1;
765 unsigned long a0x0;
766 unsigned long a1x0pa0x1;
767
768 a1x1 = a1 * (x >> 16);
769 a0x0 = a0 * (x & 0x0000FFFF);
770 a1x0pa0x1 = a1ma0 * ((x & 0x0000FFFF) - (x >> 16) ) + a1x1 + a0x0;
771 a1x0pa0x1 &= 0xFFFFFFFF;
772 a1x1 += (a1x0pa0x1 >> 16);
773 unsigned long ax = (a1x0pa0x1 << 16) & 0xFFFFFFFF;
774 a0x0 += ax;
775 if (a0x0 < ax) a1x1++;
776 x = (a1x1 << 1) + (a0x0 >> 31) + (a0x0 & 0x7FFFFFFF);
777 if (x & 0x80000000) x -= D31M1;
778 seed = x;
779 }
780
781 Real FM::Next() { NextValue(); return (double)seed / 2147483648.0; }
782
783 unsigned long FM::ulNext() { NextValue(); return seed << 1; }
784
785
786 const char* FM::Name() { return "Fishman Moore"; }
787
788 FM::FM(double s)
789 {
790 if (s>=1.0 || s<=0.0)
791 Throw(Logic_error("Newran: seed out of range"));
792 seed = (unsigned long)(s*2147483648UL);
793 Update = false;
794 }
795
796 void FM::CSFD(bool update)
797 {
798 Update = false;
799 ifstream in((Dir+"fm.txt").c_str());
800 ReadVerify(in);
801 unsigned long iseed = 0;
802 char x;
803 for (int n = 28; n >= 0; n -= 4) { in >> x; iseed |= unhex(x, n); }
804 ReadVerify(in);
805 in.close();
806 seed = iseed;
807 Update = update;
808 }
809
810 void FM::CSTD()
811 {
812 ifstream in; in.open((Dir+"lgm_mix.txt").c_str(), ios::in);
813 ReadVerify(in, true); if (in) in.close();
814 ofstream out((Dir+"fm.txt").c_str());
815 WriteVerify(out);
816 unsigned long iseed = (unsigned long)seed;
817 for (int n = 28; n >= 0; n -= 4) out << hex(iseed, n);
818 WriteVerify(out);
819 out << endl;
820 out.close();
821 }
822
823
824 FM::~FM()
825 {
826 if (Update) CSTD();
827 if (RNG == this) RNG = Dummy;
828 }
829
830 #undef D31M1
831 #undef a
832 #undef a1
833 #undef a0
834 #undef a1ma0
835
836
837
838
839
840
841 //***************** Dummy for loading at startup ************************
842
843 const char* DummyRNG::Name() { return "DummyRNG"; }
844
845 Real DummyRNG::Next()
846 {
847 Throw(Runtime_error("Newran: uniform rng not setup"));
848 #ifndef UseExceptions
849 return 0;
850 #endif
851 }
852
853 void DummyRNG::CSFD(bool)
854 {
855 Throw(Runtime_error("Newran: uniform rng not setup"));
856 }
857
858 void DummyRNG::CSTD()
859 {
860 Throw(Runtime_error("Newran: uniform rng not setup"));
861 }
862
863
864 #ifdef use_namespace
865 }
866 #endif
867
868 // Additional documentation for newran.
869 // Information that could have been included in newran.h but is put here to
870 // avoid making newran.h too complicated.
871
872 /// \fn static void Random::Set(Random &r)
873 /// Set the uniform rng to be used by subsequent calls to rngs.
874
875 /// \fn static void Random::CopySeedFromDisk(bool update=false)
876 /// Copy the seed from disk.
877 /// Boolean argument determines whether the seed stored on the disk will
878 /// be restored when the current block exits
879
880 /// \fn static void Random::CopySeedToDisk()
881 /// Copy the current value of the seed to disk.
882
883 /// \fn static void Random::SetDirectory(const char* dir)
884 /// Set the directory where seeds are stored.
885
886 /// \fn virtual Real Random::Next()
887 /// Return a new random number.
888
889 /// \fn virtual unsigned long Random::ulNext()
890 /// Return a new random number as an unsigned long.
891
892 /// \fn virtual const char* Random::Name()
893 /// Return a pointer to a character string identifying an rng.
894
895 /// \fn virtual ExtReal Random::Mean() const
896 /// Return the expectation (mean) of an rng.
897
898 /// \fn virtual ExtReal Random::Variance() const
899 /// Return the variance of an rng.
900
901
902
903
904
905 ///@}
906