2
A C-program for MT19937-64 (2004/9/29 version).
3
Coded by Takuji Nishimura and Makoto Matsumoto.
5
This is a 64-bit version of Mersenne Twister pseudorandom number
8
Before using, initialize the state by using init_genrand64(seed)
9
or init_by_array64(init_key, key_length).
11
Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
14
Redistribution and use in source and binary forms, with or without
15
modification, are permitted provided that the following conditions
18
1. Redistributions of source code must retain the above copyright
19
notice, this list of conditions and the following disclaimer.
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.
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
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.
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.
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)
58
#include "mt19937-64-unsafe.h"
62
#define MATRIX_A 0xB5026F5AA96619E9ULL
63
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
64
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
67
/* The array for the state vector */
68
static unsigned long long mt[NN];
69
/* mti==NN+1 means mt[NN] is not initialized */
72
/* initializes mt[NN] with a seed */
73
void init_genrand64_unsafe(unsigned long long seed)
76
for (mti=1; mti<NN; mti++)
77
mt[mti] = (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
80
/* generates a random number on [0, 2^64-1]-interval */
81
unsigned long long genrand64_int64_unsafe(void)
85
static unsigned long long mag01[2]={0ULL, MATRIX_A};
87
if (mti >= NN) { /* generate NN words at one time */
89
/* if init_genrand64() has not been called, */
90
/* a default initial seed is used */
92
init_genrand64_unsafe(5489ULL);
94
for (i=0;i<NN-MM;i++) {
95
x = (mt[i]&UM)|(mt[i+1]&LM);
96
mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
99
x = (mt[i]&UM)|(mt[i+1]&LM);
100
mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
102
x = (mt[NN-1]&UM)|(mt[0]&LM);
103
mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
110
x ^= (x >> 29) & 0x5555555555555555ULL;
111
x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
112
x ^= (x << 37) & 0xFFF7EEE000000000ULL;
118
/* generates a random number on [0,1)-real-interval */
119
double genrand64_real2_unsafe(void)
121
return (genrand64_int64_unsafe() >> 11) * (1.0/9007199254740992.0);