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