| 1 | /* $Header$ */cxx . . $ `<file>/tads3/vmmersenne.h`/ |
| 2 | |
| 3 | /* Copyright (c) 2012 by Michael J. Roberts. All Rights Reserved. */ |
| 4 | /* |
| 5 | Name |
| 6 | vmmersenne.h - Mersenne twister random number generator definitions |
| 7 | Function |
| 8 | |
| 9 | Notes |
| 10 | |
| 11 | Modified |
| 12 | 02/28/12 MJRoberts - Creation |
| 13 | */ |
| 14 | |
| 15 | #ifndef VMMERSENNE_H |
| 16 | #define VMMERSENNE_Hcxx . . $ `tads3/vmmersenne.h:16:9`! |
| 17 | |
| 18 | #include "t3std.h"cxx . . $ `<file>/tads3/t3std.h`/ |
| 19 | |
| 20 | |
| 21 | /* |
| 22 | * Mersenne Twister MT19937 algorithm |
| 23 | */ |
| 24 | class CVmMT19937cxx . . $ CVmMT19937# |
| 25 | { |
| 26 | public: |
| 27 | CVmMT19937cxx . . $ CVmMT19937#CVmMT19937(49f6e7a06ebc5aa8).() |
| 28 | { |
| 29 | idxcxx . . $ CVmMT19937#idx. = 0; |
| 30 | } |
| 31 | |
| 32 | /* seed from OS-generated random data */ |
| 33 | void seed_randomcxx . . $ CVmMT19937#seed_random(49f6e7a06ebc5aa8).() |
| 34 | { |
| 35 | /* fill the mt[] array with random bytes */ |
| 36 | os_gen_rand_bytes((unsigned char *)mtcxx . . $ CVmMT19937#mt., sizeof(mtcxx . . $ CVmMT19937#mt.)); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Seed from an integer value. This uses an LCG to extend the integer |
| 41 | * to fill the array. This follows the reference implementation's |
| 42 | * seeding mechanism, which lets us test against reference output. |
| 43 | */ |
| 44 | void seedcxx . . $ CVmMT19937#seed(cb2f890c2fdad230).(uint32_t s) |
| 45 | { |
| 46 | /* do a pass through the mt array with LCG values from the seed */ |
| 47 | base_seedcxx . . $ CVmMT19937#base_seed(cb2f890c2fdad230).(s); |
| 48 | |
| 49 | /* |
| 50 | * discard a few initial values to prime the pump - the MT |
| 51 | * algorithm is considered slow at getting started (i.e., it needs |
| 52 | * to run through a few iterations before it starts generating good |
| 53 | * numbers) |
| 54 | */ |
| 55 | for (int i = 0 ; i < 37 ; ++i) |
| 56 | randcxx . . $ CVmMT19937#rand(db52760e79bf9054).(); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Seed the generator from bytes. The seed can be any size; we'll use |
| 61 | * a combination of hashing and LCG generation to populate the internal |
| 62 | * state vector. |
| 63 | */ |
| 64 | void seedcxx . . $ CVmMT19937#seed(7cafb6e0818eff3b).(const char *buf, size_t len) |
| 65 | { |
| 66 | /* |
| 67 | * Use the mechanism from the reference implementation for seeding |
| 68 | * from multiple values. First do a fixed seeding of the mt array |
| 69 | * to fill it with nonzero bits. |
| 70 | */ |
| 71 | base_seedcxx . . $ CVmMT19937#base_seed(cb2f890c2fdad230).(19650218); |
| 72 | |
| 73 | /* treat the source as an array of int32s */ |
| 74 | size_t icnt = (len + 3)/4; |
| 75 | |
| 76 | /* if there's a partial int32 at the end, figure its value */ |
| 77 | uint32_t partial = 0; |
| 78 | if (icnt*4 > len) |
| 79 | { |
| 80 | int mul = 1; |
| 81 | for (size_t i = (len/4)*4 ; i < len ; ++i, mul *= 256) |
| 82 | partial += buf[i]*mul; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * do a pass over the mt array, doing the sort of LCG |
| 87 | * initialization we do with a single int seed, but folding each |
| 88 | * seed value into the running LCG feedback register |
| 89 | */ |
| 90 | int i = 1, j = 0, k = NMTcxx . . $ CVmMT19937#NMT. > icnt ? NMTcxx . . $ CVmMT19937#NMT. : icnt; |
| 91 | for ( ; k != 0 ; --k) |
| 92 | { |
| 93 | /* get the current seed (the last one might be partial) */ |
| 94 | uint32_t seed = (j+1)*4 <= (int)len ? osrp4(buf + j*4) : partial; |
| 95 | |
| 96 | /* generate an LCG value mixing in this seed value */ |
| 97 | mtcxx . . $ CVmMT19937#mt.[i] = (mtcxx . . $ CVmMT19937#mt.[i] ^ ((mtcxx . . $ CVmMT19937#mt.[i-1] ^ (mtcxx . . $ CVmMT19937#mt.[i-1] >> 30)) * 0x19660D)) |
| 98 | + seed + j; |
| 99 | |
| 100 | /* on to the next mt index, wrapping at the end */ |
| 101 | if (++i >= NMTcxx . . $ CVmMT19937#NMT.) |
| 102 | { |
| 103 | mtcxx . . $ CVmMT19937#mt.[0] = mtcxx . . $ CVmMT19937#mt.[NMTcxx . . $ CVmMT19937#NMT.-1]; |
| 104 | i = 1; |
| 105 | } |
| 106 | |
| 107 | /* on to the next seed index, wrapping at the end */ |
| 108 | if (++j >= (int)icnt) |
| 109 | j = 0; |
| 110 | } |
| 111 | |
| 112 | /* make another pass over the mt array */ |
| 113 | for (k = NMTcxx . . $ CVmMT19937#NMT.-1 ; k != 0 ; --k) |
| 114 | { |
| 115 | mtcxx . . $ CVmMT19937#mt.[i] = (mtcxx . . $ CVmMT19937#mt.[i] ^ ((mtcxx . . $ CVmMT19937#mt.[i-1] ^ (mtcxx . . $ CVmMT19937#mt.[i-1] >> 30)) * 0x5D588B65)) - i; |
| 116 | if (++i >= NMTcxx . . $ CVmMT19937#NMT.) |
| 117 | { |
| 118 | mtcxx . . $ CVmMT19937#mt.[0] = mtcxx . . $ CVmMT19937#mt.[NMTcxx . . $ CVmMT19937#NMT.-1]; |
| 119 | i = 1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* and finally, make sure the initial array isn't all zeros */ |
| 124 | mtcxx . . $ CVmMT19937#mt.[0] = 0x80000000UL; |
| 125 | |
| 126 | /* flush out the first full mt array */ |
| 127 | idxcxx . . $ CVmMT19937#idx. = 0; |
| 128 | for (i = 0 ; i <= NMTcxx . . $ CVmMT19937#NMT. ; ++i) |
| 129 | randcxx . . $ CVmMT19937#rand(db52760e79bf9054).(); |
| 130 | } |
| 131 | |
| 132 | /* generate a random number in the full uint32_t range */ |
| 133 | uint32_t randcxx . . $ CVmMT19937#rand(db52760e79bf9054).() |
| 134 | { |
| 135 | /* if we're out of untempered numbers, generate the next set */ |
| 136 | if (idxcxx . . $ CVmMT19937#idx. >= NMTcxx . . $ CVmMT19937#NMT.) |
| 137 | { |
| 138 | generate_untemperedcxx . . $ CVmMT19937#generate_untempered(49f6e7a06ebc5aa8).(); |
| 139 | idxcxx . . $ CVmMT19937#idx. = 0; |
| 140 | } |
| 141 | |
| 142 | /* generate a tempered result */ |
| 143 | uint32_t y = mtcxx . . $ CVmMT19937#mt.[idxcxx . . $ CVmMT19937#idx.]; |
| 144 | y ^= (y >> 11); |
| 145 | y ^= ((y << 7) & 0x9d2c5680); |
| 146 | y ^= ((y << 15) & 0xefc60000); |
| 147 | y ^= (y >> 18); |
| 148 | |
| 149 | /* on to the next index */ |
| 150 | ++idxcxx . . $ CVmMT19937#idx.; |
| 151 | |
| 152 | /* return the result */ |
| 153 | return y; |
| 154 | } |
| 155 | |
| 156 | /* get the size of the serialized state vector */ |
| 157 | size_t get_state_sizecxx . . $ CVmMT19937#get_state_size(fe4d2d262d4164a8).() const { return 2 + NMTcxx . . $ CVmMT19937#NMT.*4; } |
| 158 | |
| 159 | /* serialize the state into a byte buffer */ |
| 160 | void get_statecxx . . $ CVmMT19937#get_state(fda57c8427c60377).(char *buf) |
| 161 | { |
| 162 | oswp2(buf, (short)idxcxx . . $ CVmMT19937#idx.); |
| 163 | for (int i = 0, dst = 2 ; i < NMTcxx . . $ CVmMT19937#NMT. ; ++i, dst += 4) |
| 164 | oswp4(buf + dst, mtcxx . . $ CVmMT19937#mt.[i]); |
| 165 | } |
| 166 | |
| 167 | /* restore the state from a byte buffer */ |
| 168 | void put_statecxx . . $ CVmMT19937#put_state(85c52e162fed56f9).(const char *buf) |
| 169 | { |
| 170 | idxcxx . . $ CVmMT19937#idx. = osrp2(buf) % NMTcxx . . $ CVmMT19937#NMT.; |
| 171 | for (int i = 0, src = 2 ; i < NMTcxx . . $ CVmMT19937#NMT. ; ++i, src += 4) |
| 172 | mtcxx . . $ CVmMT19937#mt.[i] = osrp4(buf + src); |
| 173 | } |
| 174 | |
| 175 | protected: |
| 176 | /* basic seed generator */ |
| 177 | void base_seedcxx . . $ CVmMT19937#base_seed(cb2f890c2fdad230).(uint32_t s) |
| 178 | { |
| 179 | mtcxx . . $ CVmMT19937#mt.[0] = s; |
| 180 | for (idxcxx . . $ CVmMT19937#idx. = 1 ; idxcxx . . $ CVmMT19937#idx. < NMTcxx . . $ CVmMT19937#NMT. ; ++idxcxx . . $ CVmMT19937#idx.) |
| 181 | mtcxx . . $ CVmMT19937#mt.[idxcxx . . $ CVmMT19937#idx.] = 0x6c078965 * (mtcxx . . $ CVmMT19937#mt.[idxcxx . . $ CVmMT19937#idx.-1] ^ (mtcxx . . $ CVmMT19937#mt.[idxcxx . . $ CVmMT19937#idx.-1] >> 30)) + idxcxx . . $ CVmMT19937#idx.; |
| 182 | } |
| 183 | |
| 184 | /* generate a new set of 624 untempered numbers */ |
| 185 | void generate_untemperedcxx . . $ CVmMT19937#generate_untempered(49f6e7a06ebc5aa8).() |
| 186 | { |
| 187 | for (int i = 0 ; i < NMTcxx . . $ CVmMT19937#NMT. ; ++i) |
| 188 | { |
| 189 | uint32_t y = (mtcxx . . $ CVmMT19937#mt.[i] & 0x80000000) + (mtcxx . . $ CVmMT19937#mt.[(i+1) % NMTcxx . . $ CVmMT19937#NMT.] & 0x7fffffff); |
| 190 | mtcxx . . $ CVmMT19937#mt.[i] = mtcxx . . $ CVmMT19937#mt.[(i + 397) % NMTcxx . . $ CVmMT19937#NMT.] ^ (y >> 1); |
| 191 | if ((y & 1) != 0) |
| 192 | mtcxx . . $ CVmMT19937#mt.[i] ^= 0x9908b0df; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* internal state vector */ |
| 197 | static const int NMTcxx . . $ CVmMT19937#NMT. = 624; |
| 198 | uint32_t mtcxx . . $ CVmMT19937#mt.[NMTcxx . . $ CVmMT19937#NMT.]; |
| 199 | |
| 200 | /* current element index */ |
| 201 | int idxcxx . . $ CVmMT19937#idx.; |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | #endif /* VMMERSENNE_H */ |