| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,144 @@ |
| 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 |
+/* The code has been modified to store internal state in heap/stack |
|
| 57 |
+ * allocated memory, rather than statically allocated memory, to allow |
|
| 58 |
+ * multiple instances running in the same address space. */ |
|
| 59 |
+ |
|
| 60 |
+#include <stdlib.h> |
|
| 61 |
+#include <stdio.h> |
|
| 62 |
+#include "theft_mt.h" |
|
| 63 |
+ |
|
| 64 |
+#define NN THEFT_MT_PARAM_N |
|
| 65 |
+#define MM 156 |
|
| 66 |
+#define MATRIX_A 0xB5026F5AA96619E9ULL |
|
| 67 |
+#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */ |
|
| 68 |
+#define LM 0x7FFFFFFFULL /* Least significant 31 bits */ |
|
| 69 |
+ |
|
| 70 |
+static uint64_t genrand64_int64(struct theft_mt *r); |
|
| 71 |
+ |
|
| 72 |
+/* Heap-allocate a mersenne twister struct. */ |
|
| 73 |
+struct theft_mt *theft_mt_init(uint64_t seed) {
|
|
| 74 |
+ struct theft_mt *mt = malloc(sizeof(struct theft_mt)); |
|
| 75 |
+ if (mt == NULL) { return NULL; }
|
|
| 76 |
+ theft_mt_reset(mt, seed); |
|
| 77 |
+ return mt; |
|
| 78 |
+} |
|
| 79 |
+ |
|
| 80 |
+/* Free a heap-allocated mersenne twister struct. */ |
|
| 81 |
+void theft_mt_free(struct theft_mt *mt) {
|
|
| 82 |
+ free(mt); |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+/* initializes mt[NN] with a seed */ |
|
| 86 |
+void theft_mt_reset(struct theft_mt *mt, uint64_t seed) |
|
| 87 |
+{
|
|
| 88 |
+ mt->mt[0] = seed; |
|
| 89 |
+ uint16_t mti = 0; |
|
| 90 |
+ for (mti=1; mti<NN; mti++) {
|
|
| 91 |
+ mt->mt[mti] = (6364136223846793005ULL * |
|
| 92 |
+ (mt->mt[mti-1] ^ (mt->mt[mti-1] >> 62)) + mti); |
|
| 93 |
+ } |
|
| 94 |
+ mt->mti = mti; |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+/* Get a 64-bit random number. */ |
|
| 98 |
+uint64_t theft_mt_random(struct theft_mt *mt) {
|
|
| 99 |
+ return genrand64_int64(mt); |
|
| 100 |
+} |
|
| 101 |
+ |
|
| 102 |
+/* Generate a random number on [0,1]-real-interval. */ |
|
| 103 |
+double theft_mt_random_double(struct theft_mt *mt) |
|
| 104 |
+{
|
|
| 105 |
+ return (genrand64_int64(mt) >> 11) * (1.0/9007199254740991.0); |
|
| 106 |
+} |
|
| 107 |
+ |
|
| 108 |
+/* generates a random number on [0, 2^64-1]-interval */ |
|
| 109 |
+static uint64_t genrand64_int64(struct theft_mt *r) |
|
| 110 |
+{
|
|
| 111 |
+ int i; |
|
| 112 |
+ uint64_t x; |
|
| 113 |
+ static uint64_t mag01[2]={0ULL, MATRIX_A};
|
|
| 114 |
+ |
|
| 115 |
+ if (r->mti >= NN) { /* generate NN words at one time */
|
|
| 116 |
+ |
|
| 117 |
+ /* if init has not been called, */ |
|
| 118 |
+ /* a default initial seed is used */ |
|
| 119 |
+ if (r->mti == NN+1) |
|
| 120 |
+ theft_mt_reset(r, 5489ULL); |
|
| 121 |
+ |
|
| 122 |
+ for (i=0;i<NN-MM;i++) {
|
|
| 123 |
+ x = (r->mt[i]&UM)|(r->mt[i+1]&LM); |
|
| 124 |
+ r->mt[i] = r->mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; |
|
| 125 |
+ } |
|
| 126 |
+ for (;i<NN-1;i++) {
|
|
| 127 |
+ x = (r->mt[i]&UM)|(r->mt[i+1]&LM); |
|
| 128 |
+ r->mt[i] = r->mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; |
|
| 129 |
+ } |
|
| 130 |
+ x = (r->mt[NN-1]&UM)|(r->mt[0]&LM); |
|
| 131 |
+ r->mt[NN-1] = r->mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; |
|
| 132 |
+ |
|
| 133 |
+ r->mti = 0; |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 136 |
+ x = r->mt[r->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 |
+} |