HILA
Loading...
Searching...
No Matches
mersenne_inline.cpp
1/* A C-program for MT19937: Real number version (1998/4/6) */
2/* genrand() generates one pseudorandom real number (double) */
3/* which is uniformly distributed on [0,1]-interval, for each */
4/* call. sgenrand(seed) set initial values to the working area */
5/* of 624 words. Before genrand(), sgenrand(seed) must be */
6/* called once. (seed is any 32-bit integer except for 0). */
7/* Integer generator is obtained by modifying two lines. */
8/* Coded by Takuji Nishimura, considering the suggestions by */
9/* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
10
11/* This library is free software; you can redistribute it and/or */
12/* modify it under the terms of the GNU Library General Public */
13/* License as published by the Free Software Foundation; either */
14/* version 2 of the License, or (at your option) any later */
15/* version. */
16/* This library is distributed in the hope that it will be useful, */
17/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19/* See the GNU Library General Public License for more details. */
20/* You should have received a copy of the GNU Library General */
21/* Public License along with this library; if not, write to the */
22/* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
23/* 02111-1307 USA */
24
25/* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
26/* When you use this, send an email to: matumoto@math.keio.ac.jp */
27/* with an appropriate reference to your work. */
28
29/* REFERENCE */
30/* M. Matsumoto and T. Nishimura, */
31/* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */
32/* Pseudo-Random Number Generator", */
33/* ACM Transactions on Modeling and Computer Simulation, */
34/* Vol. 8, No. 1, January 1998, pp 3--30. */
35
36#include <stdio.h>
37#include <stdlib.h>
38
39/* Period parameters */
40#define N 624
41#define M 397
42#define MATRIX_A 0x9908b0df /* constant vector a */
43#define UPPER_MASK 0x80000000 /* most significant w-r bits */
44#define LOWER_MASK 0x7fffffff /* least significant r bits */
45
46/* Tempering parameters */
47#define TEMPERING_MASK_B 0x9d2c5680
48#define TEMPERING_MASK_C 0xefc60000
49#define TEMPERING_SHIFT_U(y) (y >> 11)
50#define TEMPERING_SHIFT_S(y) (y << 7)
51#define TEMPERING_SHIFT_T(y) (y << 15)
52#define TEMPERING_SHIFT_L(y) (y >> 18)
53
54static unsigned int mt[N]; /* the array for the state vector */
55int mersenne_i = -1; /* < 0 means mt[N] is not initialized */
56double mersenne_array[N];
57
58/* initializing the array with a NONZERO seed */
59void seed_mersenne(unsigned long seed) {
60 int mti;
61 /* setting initial seeds to mt[N] using */
62 /* the generator Line 25 of Table 1 in */
63 /* [KNUTH 1981, The Art of Computer Programming */
64 /* Vol. 2 (2nd Ed.), pp102] */
65 if (seed % 2 == 0)
66 seed++;
67 mt[0] = seed & 0xffffffff;
68 for (mti = 1; mti < N; mti++)
69 mt[mti] = (69069 * mt[mti - 1]) & 0xffffffff;
70
71 mersenne_i = 0;
72}
73
74double /* generating reals */
75/* unsigned int */ /* for integer generation */
76mersenne_generate() {
77 unsigned int y;
78 int kk;
79 static unsigned int mag01[2] = {0x0, MATRIX_A};
80 /* mag01[x] = x * MATRIX_A for x=0,1 */
81
82 if (mersenne_i < 0) { /* if sgenrand() has not been called, */
83 printf("DUMMY: you did not seed the generator!\n");
84 exit(1);
85 }
86
87 /* generate N words at one time */
88
89 for (kk = 0; kk < N - M; kk++) {
90 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
91 mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1];
92 }
93 for (; kk < N - 1; kk++) {
94 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
95 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1];
96 }
97 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
98 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1];
99
100 for (kk = 0; kk < N; kk++) {
101 y = mt[kk];
102 y ^= TEMPERING_SHIFT_U(y);
103 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
104 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
105 y ^= TEMPERING_SHIFT_L(y);
106 mersenne_array[kk] =
107 (double)y * 2.3283064365386963e-10; /* reals: interval [0,1) */
108 }
109
110 mersenne_i = N;
111 return (mersenne_array[--mersenne_i]);
112 /* return y; */ /* for integer generation */
113}