Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | A C-program for MT19937, with initialization improved 2002/1/26. |
3 | | Coded by Takuji Nishimura and Makoto Matsumoto. |
4 | | |
5 | | Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, |
6 | | All rights reserved. |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | |
12 | | 1. Redistributions of source code must retain the above copyright |
13 | | notice, this list of conditions and the following disclaimer. |
14 | | |
15 | | 2. Redistributions in binary form must reproduce the above copyright |
16 | | notice, this list of conditions and the following disclaimer in the |
17 | | documentation and/or other materials provided with the distribution. |
18 | | |
19 | | 3. The names of its contributors may not be used to endorse or promote |
20 | | products derived from this software without specific prior written |
21 | | permission. |
22 | | |
23 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
26 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
27 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
28 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
29 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
30 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
31 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
32 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
33 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34 | | |
35 | | |
36 | | Any feedback is very welcome. |
37 | | http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html |
38 | | email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) |
39 | | */ |
40 | | |
41 | | #include <assert.h> |
42 | | #include <stdio.h> |
43 | | #include <stdlib.h> |
44 | | #include "mutator_aux.h" |
45 | | |
46 | | #define init_genrand prng_init |
47 | | #define genrand_int32 prng_uint32 |
48 | | |
49 | | /* Period parameters */ |
50 | 69.0M | #define N 624 |
51 | 33.7M | #define M 397 |
52 | 18.0M | #define MATRIX_A 0x9908b0dfUL /* constant vector a */ |
53 | 24.7M | #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ |
54 | 24.7M | #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ |
55 | | |
56 | | int prng_up = 0; |
57 | | static unsigned long mt[N]; /* the array for the state vector */ |
58 | | static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ |
59 | | |
60 | | /* initializes mt[N] with a seed */ |
61 | | void init_genrand(unsigned long s) |
62 | 16.8k | { |
63 | 16.8k | mt[0]= s & 0xffffffffUL; |
64 | 10.5M | for (mti=1; mti<N; mti++) { |
65 | 10.5M | mt[mti] = |
66 | 10.5M | (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + |
67 | 10.5M | (unsigned long)mti); |
68 | | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
69 | | /* In the previous versions, MSBs of the seed affect */ |
70 | | /* only MSBs of the array mt[]. */ |
71 | | /* 2002/01/09 modified by Makoto Matsumoto */ |
72 | 10.5M | mt[mti] &= 0xffffffffUL; |
73 | | /* for >32 bit machines */ |
74 | 10.5M | } |
75 | 16.8k | prng_up = 1; |
76 | 16.8k | } |
77 | | |
78 | | /* generates a random number on [0,0xffffffff]-interval */ |
79 | | unsigned long genrand_int32(void) |
80 | 18.0M | { |
81 | 18.0M | unsigned long y; |
82 | 18.0M | static unsigned long mag01[2]={0x0UL, MATRIX_A}; |
83 | | /* mag01[x] = x * MATRIX_A for x=0,1 */ |
84 | | |
85 | 18.0M | if (mti >= N) { /* generate N words at one time */ |
86 | 39.5k | int kk; |
87 | | |
88 | 39.5k | assert(mti != N+1); |
89 | | |
90 | 9.02M | for (kk=0;kk<N-M;kk++) { |
91 | 8.98M | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
92 | 8.98M | mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
93 | 8.98M | } |
94 | 15.7M | for (;kk<N-1;kk++) { |
95 | 15.6M | y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); |
96 | 15.6M | mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
97 | 15.6M | } |
98 | 39.5k | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); |
99 | 39.5k | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; |
100 | | |
101 | 39.5k | mti = 0; |
102 | 39.5k | } |
103 | | |
104 | 0 | y = mt[mti++]; |
105 | | |
106 | | /* Tempering */ |
107 | 18.0M | y ^= (y >> 11); |
108 | 18.0M | y ^= (y << 7) & 0x9d2c5680UL; |
109 | 18.0M | y ^= (y << 15) & 0xefc60000UL; |
110 | 18.0M | y ^= (y >> 18); |
111 | | |
112 | 18.0M | return y; |
113 | 18.0M | } |