libdspl-2.0
Digital Signal Processing Algorithm Library
mt19937.c
1/*
2 A C-program for MT19937-64 (2004/9/29 version).
3 Coded by Takuji Nishimura and Makoto Matsumoto.
4
5 This is a 64-bit version of Mersenne Twister pseudorandom number
6 generator.
7
8 Before using, initialize the state by using init_genrand64(seed)
9 or init_by_array64(init_key, key_length).
10
11 Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions
16 are met:
17
18 1. Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
20
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24
25 3. The names of its contributors may not be used to endorse or promote
26 products derived from this software without specific prior written
27 permission.
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 References:
42 T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
43 ACM Transactions on Modeling and
44 Computer Simulation 10. (2000) 348--357.
45 M. Matsumoto and T. Nishimura,
46 ``Mersenne Twister: a 623-dimensionally equidistributed
47 uniform pseudorandom number generator''
48 ACM Transactions on Modeling and
49 Computer Simulation 8. (Jan. 1998) 3--30.
50
51 Any feedback is very welcome.
52 http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
53 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
54*/
55
56
57#include <stdio.h>
58#include "dspl.h"
59#include "mt19937.h"
60
61#define NN RAND_MT19937_NN
62#define MM 156
63#define MATRIX_A 0xB5026F5AA96619E9ULL
64#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
65#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
66
67
68/* initializes mt[NN] with a seed */
69void mt19937_init_genrand64(unsigned long long seed, random_t* prnd)
70{
71 unsigned long long *mt = prnd->mt19937_mt;
72 int mti = prnd->mt19937_mti;
73
74 mt[0] = seed;
75 for (mti=1; mti<NN; mti++)
76 mt[mti] = (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
77
78 prnd->mt19937_mti = mti;
79}
80
81/* initialize by an array with array-length */
82/* init_key is the array for initializing keys */
83/* key_length is its length */
84void mt19937_init_by_array64(unsigned long long init_key[],
85 unsigned long long key_length,
86 random_t* prnd)
87{
88 unsigned long long i, j, k;
89 unsigned long long *mt = prnd->mt19937_mt;
90 /* int mti = prnd->mt19937_mti; */
91
92 mt19937_init_genrand64(19650218ULL, prnd);
93 i=1; j=0;
94 k = (NN>key_length ? NN : key_length);
95 for (; k; k--) {
96 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 3935559000370003845ULL))
97 + init_key[j] + j; /* non linear */
98 i++; j++;
99 if (i>=NN) { mt[0] = mt[NN-1]; i=1; }
100 if (j>=key_length) j=0;
101 }
102 for (k=NN-1; k; k--) {
103 /* non linear */
104 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 2862933555777941757ULL)) - i;
105
106 i++;
107 if (i>=NN)
108 {
109 mt[0] = mt[NN-1];
110 i=1;
111 }
112 }
113 mt[0] = 1ULL << 63; /* MSB is 1; assuring non-zero initial array */
114}
115
116
117/* generates a random number on [0, 2^64-1]-interval */
118unsigned long long mt19937_genrand64_int64(random_t* prnd)
119{
120 int i;
121 unsigned long long x;
122 static unsigned long long mag01[2]={0ULL, MATRIX_A};
123 unsigned long long *mt = prnd->mt19937_mt;
124 int mti = prnd->mt19937_mti;
125
126 if (mti >= NN) { /* generate NN words at one time */
127
128 /* if init_genrand64() has not been called, */
129 /* a default initial seed is used */
130 if (mti == NN+1)
131 mt19937_init_genrand64(5489ULL, prnd);
132
133 for (i=0;i<NN-MM;i++) {
134 x = (mt[i]&UM)|(mt[i+1]&LM);
135 mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
136 }
137 for (;i<NN-1;i++) {
138 x = (mt[i]&UM)|(mt[i+1]&LM);
139 mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
140 }
141 x = (mt[NN-1]&UM)|(mt[0]&LM);
142 mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
143
144 mti = 0;
145 }
146
147 x = mt[mti++];
148
149 x ^= (x >> 29) & 0x5555555555555555ULL;
150 x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
151 x ^= (x << 37) & 0xFFF7EEE000000000ULL;
152 x ^= (x >> 43);
153
154 prnd->mt19937_mti = mti;
155 return x;
156}
157
158
159/* generates a random number on [0, 2^63-1]-interval */
160long long mt19937_genrand64_int63(random_t* prnd)
161{
162 return (long long)(mt19937_genrand64_int64(prnd) >> 1);
163}
164
165
166/* generates a random number on [0,1]-real-interval */
167double mt19937_genrand64_real1(random_t* prnd)
168{
169 return (mt19937_genrand64_int64(prnd) >> 11) * (1.0/9007199254740991.0);
170}
171
172
173/* generates a random number on [0,1)-real-interval */
174double mt19937_genrand64_real2(random_t* prnd)
175{
176 return (mt19937_genrand64_int64(prnd) >> 11) * (1.0/9007199254740992.0);
177}
178
179
180/* generates a random number on (0,1)-real-interval */
181double mt19937_init_genrand64_real3(random_t* prnd)
182{
183 return ((mt19937_genrand64_int64(prnd) >> 12) + 0.5) * (1.0/4503599627370496.0);
184}
Definition: dspl.h:350
int mt19937_mti
Definition: dspl.h:358
unsigned long long mt19937_mt[RAND_MT19937_NN]
Definition: dspl.h:357