Debian Codegraph @

libutil/mt19937ar/mt19937ar.c

raw
1 /*
2 A C-program for MT19937, with initialization improved 2002/1/26.
3 Coded by Takuji Nishimura and Makoto Matsumoto.
4
5 Before using, initialize the state by using init_genrand(seed)
6 or init_by_array(init_key, key_length).
7
8 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
9 All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17
18 2. Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 3. The names of its contributors may not be used to endorse or promote
23 products derived from this software without specific prior written
24 permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38
39 Any feedback is very welcome.
40 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
41 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
42 */
43
44 #include "mt19937ar.h"
45
46 #include <stdlib.h>
47 #include <math.h>
48
49
50 // Buried in genrand_in32 was this:
51 // if init_genrand() has not been called,
52 // a default initial seed is used
53 //
54 // if (ctx->mti == N+1)
55 // init_genrand(5489UL);
56 //
57 // But we don't need that anymore, as we require for
58 // thread-safety that init_genrand be called.
59
60
61
62
63 // initialize with a single seed
64 mt_s*
65 mtInit(uint32 s) {
66 mt_s *ctx = (mt_s *)malloc(sizeof(mt_s));
67 if (ctx == NULL)
68 return(NULL);
69
70 ctx->mt[0] = s;
71
72 // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.
73 // In the previous versions, MSBs of the seed affect
74 // only MSBs of the array mt[].
75 // 2002/01/09 modified by Makoto Matsumoto
76
77 for (ctx->mti=1; ctx->mti<MT_N; ctx->mti++)
78 ctx->mt[ctx->mti] = (1812433253UL * (ctx->mt[ctx->mti-1] ^ (ctx->mt[ctx->mti-1] >> 30)) + ctx->mti);
79
80 ctx->mag01[0] = uint32ZERO;
81 ctx->mag01[1] = MT_MATRIX_A;
82
83 return(ctx);
84 }
85
86
87
88
89 /* initialize by an array with array-length */
90 /* init_key is the array for initializing keys */
91 /* key_length is its length */
92 /* slight change for C++, 2004/2/26 */
93 mt_s*
94 mtInitArray(uint32 *init_key, uint32 key_length) {
95
96 mt_s *ctx = mtInit(19650218UL);
97 int i = 1;
98 int j = 0;
99 int k = (MT_N > key_length ? MT_N : key_length);
100
101 for (; k; k--) {
102 ctx->mt[i] = (ctx->mt[i] ^ ((ctx->mt[i-1] ^ (ctx->mt[i-1] >> 30)) * 1664525UL)) + init_key[j] + j; /* non linear */
103 i++;
104 j++;
105 if (i >= MT_N) {
106 ctx->mt[0] = ctx->mt[MT_N-1];
107 i=1;
108 }
109 if (j >= key_length)
110 j=0;
111 }
112 for (k=MT_N-1; k; k--) {
113 ctx->mt[i] = (ctx->mt[i] ^ ((ctx->mt[i-1] ^ (ctx->mt[i-1] >> 30)) * 1566083941UL)) - i; /* non linear */
114 i++;
115 if (i>=MT_N) {
116 ctx->mt[0] = ctx->mt[MT_N-1];
117 i=1;
118 }
119 }
120
121 ctx->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
122 return(ctx);
123 }
124
125
126
127 /* generates a random number on [0,0xffffffff]-interval */
128 uint32
129 mtRandom32(mt_s *ctx) {
130 uint32 y;
131
132 // generate MT_N words at one time
133 //
134 if (ctx->mti >= MT_N) {
135 int kk;
136
137 for (kk=0; kk < MT_N - MT_M; kk++) {
138 y = (ctx->mt[kk] & MT_UPPER_MASK) | (ctx->mt[kk+1] & MT_LOWER_MASK);
139 ctx->mt[kk] = ctx->mt[kk + MT_M] ^ (y >> 1) ^ ctx->mag01[y & uint32ONE];
140 }
141 for (; kk < MT_N-1; kk++) {
142 y = (ctx->mt[kk] & MT_UPPER_MASK) | (ctx->mt[kk + 1] & MT_LOWER_MASK);
143 ctx->mt[kk] = ctx->mt[kk + (MT_M - MT_N)] ^ (y >> 1) ^ ctx->mag01[y & uint32ONE];
144 }
145 y = (ctx->mt[MT_N-1] & MT_UPPER_MASK) | (ctx->mt[0] & MT_LOWER_MASK);
146 ctx->mt[MT_N-1] = ctx->mt[MT_M-1] ^ (y >> 1) ^ ctx->mag01[y & uint32ONE];
147
148 ctx->mti = 0;
149 }
150
151 y = ctx->mt[ctx->mti++];
152
153 /* Tempering */
154 y ^= (y >> 11);
155 y ^= (y << 7) & 0x9d2c5680UL;
156 y ^= (y << 15) & 0xefc60000UL;
157 y ^= (y >> 18);
158
159 return y;
160 }
161
162
163 // generates a random number on gaussian distribution with 0 median and 1 std.dev.
164 double
165 mtRandomGaussian(mt_s *mt) {
166 double x1=0, x2=0, w=0, y1=0, y2=0;
167
168 // from http://www.taygeta.com/random/gaussian.html
169 //
170 // supposedly equivalent to
171 //
172 // y1 = sqrt(-2*ln(x1)) cos(2*pi*x2)
173 // y2 = sqrt(-2*ln(x1)) sin(2*pi*x2)
174 //
175 // but stable when x1 close to zero
176
177 do {
178 x1 = 2.0 * mtRandomRealClosed(mt) - 1.0;
179 x2 = 2.0 * mtRandomRealClosed(mt) - 1.0;
180 w = x1 * x1 + x2 * x2;
181 } while (w >= 1.0);
182
183 w = sqrt( (-2.0 * log(w)) / w);
184
185 y1 = x1 * w;
186 y2 = x2 * w;
187
188 return(y1);
189 }