Branch data Line data Source code
1 : : /* A C-program for MT19937: Integer version */
2 : : /* genrand() generates one pseudorandom unsigned integer (32bit) */
3 : : /* which is uniformly distributed among 0 to 2^32-1 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 : : /* Coded by Takuji Nishimura, considering the suggestions by */
8 : : /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
9 : :
10 : : /* This library is free software; you can redistribute it and/or */
11 : : /* modify it under the terms of the GNU Library General Public */
12 : : /* License as published by the Free Software Foundation; either */
13 : : /* version 2 of the License, or (at your option) any later */
14 : : /* version. */
15 : : /* This library is distributed in the hope that it will be useful, */
16 : : /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
17 : : /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
18 : : /* See the GNU Library General Public License for more details. */
19 : : /* You should have received a copy of the GNU Library General */
20 : : /* Public License along with this library; if not, write to the */
21 : : /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */
22 : : /* 02111-1307 USA */
23 : :
24 : : /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. */
25 : : /* When you use this, send an email to: matumoto@math.keio.ac.jp */
26 : : /* with an appropriate reference to your work. */
27 : :
28 : : /* Period parameters */
29 : : #define N 624
30 : : #define M 397
31 : : #define MATRIX_A 0x9908b0df /* constant vector a */
32 : : #define UPPER_MASK 0x80000000 /* most significant w-r bits */
33 : : #define LOWER_MASK 0x7fffffff /* least significant r bits */
34 : :
35 : : /* Tempering parameters */
36 : : #define TEMPERING_MASK_B 0x9d2c5680
37 : : #define TEMPERING_MASK_C 0xefc60000
38 : : #define TEMPERING_SHIFT_U(y) (y >> 11)
39 : : #define TEMPERING_SHIFT_S(y) (y << 7)
40 : : #define TEMPERING_SHIFT_T(y) (y << 15)
41 : : #define TEMPERING_SHIFT_L(y) (y >> 18)
42 : :
43 : : static unsigned long mt[N]; /* the array for the state vector */
44 : : static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
45 : :
46 : : /* initializing the array with a NONZERO seed */
47 : : void
48 : 5 : sgenrand(seed)
49 : : unsigned long seed;
50 : : {
51 : : /* setting initial seeds to mt[N] using */
52 : : /* the generator Line 25 of Table 1 in */
53 : : /* [KNUTH 1981, The Art of Computer Programming */
54 : : /* Vol. 2 (2nd Ed.), pp102] */
55 : 5 : mt[0]= seed & 0xffffffff;
56 [ + + ]: 3120 : for (mti=1; mti<N; mti++)
57 : 3115 : mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
58 : 5 : }
59 : :
60 : : unsigned long
61 : 4083 : genrand()
62 : : {
63 : : unsigned long y;
64 : : static unsigned long mag01[2]={0x0, MATRIX_A
65 : : };
66 : : /* mag01[x] = x * MATRIX_A for x=0,1 */
67 : :
68 [ + + ]: 4083 : if (mti >= N) { /* generate N words at one time */
69 : : int kk;
70 : :
71 [ - + ]: 10 : if (mti == N+1) /* if sgenrand() has not been called, */
72 : 0 : sgenrand(4357); /* a default initial seed is used */
73 : :
74 [ + + ]: 2280 : for (kk=0;kk<N-M;kk++) {
75 : 2270 : y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
76 : 2270 : mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
77 : : }
78 [ + + ]: 3970 : for (;kk<N-1;kk++) {
79 : 3960 : y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
80 : 3960 : mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
81 : : }
82 : 10 : y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
83 : 10 : mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
84 : :
85 : 10 : mti = 0;
86 : : }
87 : :
88 : 4083 : y = mt[mti++];
89 : 4083 : y ^= TEMPERING_SHIFT_U(y);
90 : 4083 : y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
91 : 4083 : y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
92 : 4083 : y ^= TEMPERING_SHIFT_L(y);
93 : :
94 : 4083 : return y;
95 : : }
|