|
| 1 | + |
| 2 | +MSEED = 161803398 |
| 3 | +Int32MinValue = -2147483648 |
| 4 | +Int32MaxValue = 2147483647 |
| 5 | +MBIG = Int32MaxValue |
| 6 | + |
| 7 | + |
| 8 | +def sampel_seed_1(seed): |
| 9 | + if seed == Int32MinValue: |
| 10 | + subtraction = Int32MinValue |
| 11 | + else: |
| 12 | + subtraction = abs(seed) |
| 13 | + mj = MSEED - subtraction |
| 14 | + SeedArray = [0 for i in range(56)] |
| 15 | + SeedArray[55] = mj |
| 16 | + mk = 1 |
| 17 | + |
| 18 | + for i in range(1, 55): |
| 19 | + ii = (21 * i) % 55 |
| 20 | + SeedArray[ii] = mk |
| 21 | + mk = mj - mk |
| 22 | + if mk < 0: |
| 23 | + mk += MBIG |
| 24 | + mj = SeedArray[ii] |
| 25 | + |
| 26 | + for k in range(1, 5): |
| 27 | + for i in range(1, 56): |
| 28 | + m = 1 + (i + 30) % 55 |
| 29 | + SeedArray[i] -= SeedArray[m] |
| 30 | + if SeedArray[i] < 0: |
| 31 | + SeedArray[i] += MBIG |
| 32 | + inext = 0 |
| 33 | + inextp = 21 |
| 34 | + Seed = 1 |
| 35 | + |
| 36 | + locINext = inext + 1 |
| 37 | + locINextp = inextp + 1 |
| 38 | + |
| 39 | + if locINext >= 56: |
| 40 | + locINext = 1 |
| 41 | + |
| 42 | + if locINextp >= 56: |
| 43 | + locINextp = 1 |
| 44 | + |
| 45 | + retVal = SeedArray[locINext]-SeedArray[locINextp] |
| 46 | + |
| 47 | + if retVal == MBIG: |
| 48 | + retVal -= 1 |
| 49 | + |
| 50 | + if retVal < 0: |
| 51 | + retVal += MBIG |
| 52 | + |
| 53 | + SeedArray[locINext] = retVal |
| 54 | + |
| 55 | + inext = locINext |
| 56 | + inextp = locINextp |
| 57 | + return retVal |
| 58 | + |
| 59 | +def test_sampel_seed_1(): |
| 60 | + import json |
| 61 | + with open("tests.json") as fp: |
| 62 | + test_data = json.load(fp) |
| 63 | + |
| 64 | + for _seed, rands in test_data.items(): |
| 65 | + seed = int(_seed) |
| 66 | + r = rands[0] |
| 67 | + my_r = sampel_seed_1(seed) |
| 68 | + # print("sample(%d) == %d should be %d" % (seed, my_r, r)) |
| 69 | + if my_r != r: |
| 70 | + raise Exception("sample_seed_1 test failed on\n sample(%d) == %d should be %d" % (seed, my_r, r)) |
| 71 | + |
| 72 | + |
| 73 | +def sampel_seed_2(seed): |
| 74 | + # same as 1 but returns array |
| 75 | + if seed == Int32MinValue: |
| 76 | + subtraction = Int32MinValue |
| 77 | + else: |
| 78 | + subtraction = abs(seed) |
| 79 | + mj = MSEED - subtraction |
| 80 | + SeedArray = [0 for i in range(56)] |
| 81 | + SeedArray[55] = mj |
| 82 | + mk = 1 |
| 83 | + |
| 84 | + for i in range(1, 55): |
| 85 | + ii = (21 * i) % 55 |
| 86 | + SeedArray[ii] = mk |
| 87 | + mk = mj - mk |
| 88 | + if mk < 0: |
| 89 | + mk += MBIG |
| 90 | + mj = SeedArray[ii] |
| 91 | + |
| 92 | + for k in range(1, 5): |
| 93 | + for i in range(1, 56): |
| 94 | + m = 1 + (i + 30) % 55 |
| 95 | + SeedArray[i] -= SeedArray[m] |
| 96 | + if SeedArray[i] < 0: |
| 97 | + SeedArray[i] += MBIG |
| 98 | + inext = 0 |
| 99 | + inextp = 21 |
| 100 | + |
| 101 | + ret_array = [] |
| 102 | + for i in range(34): |
| 103 | + locINext = inext + 1 |
| 104 | + locINextp = inextp + 1 |
| 105 | + |
| 106 | + if locINext >= 56 or locINextp >= 56: |
| 107 | + raise Exception("Shouldn't ever wrap") |
| 108 | + |
| 109 | + retVal = SeedArray[locINext] - SeedArray[locINextp] |
| 110 | + |
| 111 | + if retVal == MBIG: |
| 112 | + retVal -= 1 |
| 113 | + |
| 114 | + if retVal < 0: |
| 115 | + retVal += MBIG |
| 116 | + |
| 117 | + SeedArray[locINext] = retVal |
| 118 | + ret_array.append(retVal) |
| 119 | + |
| 120 | + inext = locINext |
| 121 | + inextp = locINextp |
| 122 | + |
| 123 | + return ret_array |
| 124 | + |
| 125 | + |
| 126 | +def test_sampel_seed_2(): |
| 127 | + import json |
| 128 | + with open("tests.json") as fp: |
| 129 | + test_data = json.load(fp) |
| 130 | + |
| 131 | + for _seed, rands in test_data.items(): |
| 132 | + seed = int(_seed) |
| 133 | + my_rands = sampel_seed_2(seed) |
| 134 | + for ri, my_r in enumerate(my_rands): |
| 135 | + r = rands[ri] |
| 136 | + # print("sample(%d) == %d should be %d" % (seed, my_r, r)) |
| 137 | + if my_r != r: |
| 138 | + raise Exception("sample_seed_1 test failed on\n sample(%d)[%d] == %d should be %d" % (seed, ri, my_r, r)) |
| 139 | + |
| 140 | + |
| 141 | +def sampel_seed_3(seed): |
| 142 | + # same as 2 but unrapping the sample section |
| 143 | + if seed == Int32MinValue: |
| 144 | + subtraction = Int32MinValue |
| 145 | + else: |
| 146 | + subtraction = abs(seed) |
| 147 | + mj = MSEED - subtraction |
| 148 | + SeedArray = [0 for i in range(56)] |
| 149 | + SeedArray[55] = mj |
| 150 | + mk = 1 |
| 151 | + |
| 152 | + for i in range(1, 55): |
| 153 | + ii = (21 * i) % 55 |
| 154 | + SeedArray[ii] = mk |
| 155 | + mk = mj - mk |
| 156 | + if mk < 0: |
| 157 | + mk += MBIG |
| 158 | + mj = SeedArray[ii] |
| 159 | + |
| 160 | + for k in range(1, 5): |
| 161 | + for i in range(1, 56): |
| 162 | + m = 1 + (i + 30) % 55 |
| 163 | + SeedArray[i] -= SeedArray[m] |
| 164 | + if SeedArray[i] < 0: |
| 165 | + SeedArray[i] += MBIG |
| 166 | + """ |
| 167 | + SAMPLE Section |
| 168 | + """ |
| 169 | + ret_0 = (SeedArray[1] - SeedArray[22]) % MBIG |
| 170 | + ret_1 = (SeedArray[2] - SeedArray[23]) % MBIG |
| 171 | + ret_2 = (SeedArray[3] - SeedArray[24]) % MBIG |
| 172 | + ret_3 = (SeedArray[4] - SeedArray[25]) % MBIG |
| 173 | + ret_4 = (SeedArray[5] - SeedArray[26]) % MBIG |
| 174 | + ret_5 = (SeedArray[6] - SeedArray[27]) % MBIG |
| 175 | + ret_6 = (SeedArray[7] - SeedArray[28]) % MBIG |
| 176 | + ret_7 = (SeedArray[8] - SeedArray[29]) % MBIG |
| 177 | + ret_8 = (SeedArray[9] - SeedArray[30]) % MBIG |
| 178 | + ret_9 = (SeedArray[10] - SeedArray[31]) % MBIG |
| 179 | + ret10 = (SeedArray[11] - SeedArray[32]) % MBIG |
| 180 | + ret11 = (SeedArray[12] - SeedArray[33]) % MBIG |
| 181 | + ret12 = (SeedArray[13] - SeedArray[34]) % MBIG |
| 182 | + ret13 = (SeedArray[14] - SeedArray[35]) % MBIG |
| 183 | + ret14 = (SeedArray[15] - SeedArray[36]) % MBIG |
| 184 | + ret15 = (SeedArray[16] - SeedArray[37]) % MBIG |
| 185 | + ret16 = (SeedArray[17] - SeedArray[38]) % MBIG |
| 186 | + ret17 = (SeedArray[18] - SeedArray[39]) % MBIG |
| 187 | + ret18 = (SeedArray[19] - SeedArray[40]) % MBIG |
| 188 | + ret19 = (SeedArray[20] - SeedArray[41]) % MBIG |
| 189 | + ret20 = (SeedArray[21] - SeedArray[42]) % MBIG |
| 190 | + ret21 = (SeedArray[22] - SeedArray[43]) % MBIG |
| 191 | + ret22 = (SeedArray[23] - SeedArray[44]) % MBIG |
| 192 | + ret23 = (SeedArray[24] - SeedArray[45]) % MBIG |
| 193 | + ret24 = (SeedArray[25] - SeedArray[46]) % MBIG |
| 194 | + ret25 = (SeedArray[26] - SeedArray[47]) % MBIG |
| 195 | + ret26 = (SeedArray[27] - SeedArray[48]) % MBIG |
| 196 | + ret27 = (SeedArray[28] - SeedArray[49]) % MBIG |
| 197 | + ret28 = (SeedArray[29] - SeedArray[50]) % MBIG |
| 198 | + ret29 = (SeedArray[30] - SeedArray[51]) % MBIG |
| 199 | + ret30 = (SeedArray[31] - SeedArray[52]) % MBIG |
| 200 | + ret31 = (SeedArray[32] - SeedArray[53]) % MBIG |
| 201 | + ret32 = (SeedArray[33] - SeedArray[54]) % MBIG |
| 202 | + ret33 = (SeedArray[34] - SeedArray[55]) % MBIG |
| 203 | + |
| 204 | + return [ret_0, ret_1, ret_2, ret_3, ret_4, ret_5, ret_6, ret_7, ret_8, |
| 205 | + ret_9, ret10, ret11, ret12, ret13, ret14, ret15, ret16, ret17, |
| 206 | + ret18, ret19, ret20, ret21, ret22, ret23, ret24, ret25, ret26, |
| 207 | + ret27, ret28, ret29, ret30, ret31, ret32, ret33] |
| 208 | + |
| 209 | +def test_sampel_seed_3(): |
| 210 | + import json |
| 211 | + with open("tests.json") as fp: |
| 212 | + test_data = json.load(fp) |
| 213 | + |
| 214 | + for _seed, rands in test_data.items(): |
| 215 | + seed = int(_seed) |
| 216 | + my_rands = sampel_seed_3(seed) |
| 217 | + for ri, my_r in enumerate(my_rands): |
| 218 | + r = rands[ri] |
| 219 | + # print("sample(%d) == %d should be %d" % (seed, my_r, r)) |
| 220 | + if my_r != r: |
| 221 | + raise Exception("sample_seed_1 test failed on\n sample(%d)[%d] == %d should be %d" % (seed, ri, my_r, r)) |
| 222 | + |
| 223 | +def sampel_seed_4(seed): |
| 224 | + # same as 2 but unrapping the sample section |
| 225 | + if seed == Int32MinValue: |
| 226 | + subtraction = Int32MinValue |
| 227 | + else: |
| 228 | + subtraction = abs(seed) |
| 229 | + mj = MSEED - subtraction |
| 230 | + SeedArray = [0 for i in range(56)] |
| 231 | + SeedArray[55] = mj |
| 232 | + mk = 1 |
| 233 | + |
| 234 | + for i in range(1, 55): |
| 235 | + ii = (21 * i) % 55 |
| 236 | + SeedArray[ii] = mk |
| 237 | + mk = mj - mk |
| 238 | + if mk < 0: |
| 239 | + mk += MBIG |
| 240 | + mj = SeedArray[ii] |
| 241 | + |
| 242 | + for k in range(1, 4): # <-- one less loop then sampel_seed_3 |
| 243 | + for i in range(1, 56): |
| 244 | + m = 1 + (i + 30) % 55 |
| 245 | + SeedArray[i] -= SeedArray[m] |
| 246 | + if SeedArray[i] < 0: |
| 247 | + SeedArray[i] += MBIG |
| 248 | + |
| 249 | + # that loop is put here, unwound |
| 250 | + for i in range(1, 56): |
| 251 | + m = 1 + (i + 30) % 55 |
| 252 | + print("SeedArray[%d] -= SeedArray[%d]" % (i, m)) |
| 253 | + SeedArray[i] -= SeedArray[m] |
| 254 | + if SeedArray[i] < 0: |
| 255 | + SeedArray[i] += MBIG |
| 256 | + """ |
| 257 | + SAMPLE Section |
| 258 | + """ |
| 259 | + ret_0 = (SeedArray[1] - SeedArray[22]) % MBIG |
| 260 | + ret_1 = (SeedArray[2] - SeedArray[23]) % MBIG |
| 261 | + ret_2 = (SeedArray[3] - SeedArray[24]) % MBIG |
| 262 | + ret_3 = (SeedArray[4] - SeedArray[25]) % MBIG |
| 263 | + ret_4 = (SeedArray[5] - SeedArray[26]) % MBIG |
| 264 | + ret_5 = (SeedArray[6] - SeedArray[27]) % MBIG |
| 265 | + ret_6 = (SeedArray[7] - SeedArray[28]) % MBIG |
| 266 | + ret_7 = (SeedArray[8] - SeedArray[29]) % MBIG |
| 267 | + ret_8 = (SeedArray[9] - SeedArray[30]) % MBIG |
| 268 | + ret_9 = (SeedArray[10] - SeedArray[31]) % MBIG |
| 269 | + ret10 = (SeedArray[11] - SeedArray[32]) % MBIG |
| 270 | + ret11 = (SeedArray[12] - SeedArray[33]) % MBIG |
| 271 | + ret12 = (SeedArray[13] - SeedArray[34]) % MBIG |
| 272 | + ret13 = (SeedArray[14] - SeedArray[35]) % MBIG |
| 273 | + ret14 = (SeedArray[15] - SeedArray[36]) % MBIG |
| 274 | + ret15 = (SeedArray[16] - SeedArray[37]) % MBIG |
| 275 | + ret16 = (SeedArray[17] - SeedArray[38]) % MBIG |
| 276 | + ret17 = (SeedArray[18] - SeedArray[39]) % MBIG |
| 277 | + ret18 = (SeedArray[19] - SeedArray[40]) % MBIG |
| 278 | + ret19 = (SeedArray[20] - SeedArray[41]) % MBIG |
| 279 | + ret20 = (SeedArray[21] - SeedArray[42]) % MBIG |
| 280 | + ret21 = (SeedArray[22] - SeedArray[43]) % MBIG |
| 281 | + ret22 = (SeedArray[23] - SeedArray[44]) % MBIG |
| 282 | + ret23 = (SeedArray[24] - SeedArray[45]) % MBIG |
| 283 | + ret24 = (SeedArray[25] - SeedArray[46]) % MBIG |
| 284 | + ret25 = (SeedArray[26] - SeedArray[47]) % MBIG |
| 285 | + ret26 = (SeedArray[27] - SeedArray[48]) % MBIG |
| 286 | + ret27 = (SeedArray[28] - SeedArray[49]) % MBIG |
| 287 | + ret28 = (SeedArray[29] - SeedArray[50]) % MBIG |
| 288 | + ret29 = (SeedArray[30] - SeedArray[51]) % MBIG |
| 289 | + ret30 = (SeedArray[31] - SeedArray[52]) % MBIG |
| 290 | + ret31 = (SeedArray[32] - SeedArray[53]) % MBIG |
| 291 | + ret32 = (SeedArray[33] - SeedArray[54]) % MBIG |
| 292 | + ret33 = (SeedArray[34] - SeedArray[55]) % MBIG |
| 293 | + |
| 294 | + return [ret_0, ret_1, ret_2, ret_3, ret_4, ret_5, ret_6, ret_7, ret_8, |
| 295 | + ret_9, ret10, ret11, ret12, ret13, ret14, ret15, ret16, ret17, |
| 296 | + ret18, ret19, ret20, ret21, ret22, ret23, ret24, ret25, ret26, |
| 297 | + ret27, ret28, ret29, ret30, ret31, ret32, ret33] |
| 298 | + |
| 299 | +def test_sampel_seed_4(): |
| 300 | + import json |
| 301 | + with open("tests.json") as fp: |
| 302 | + test_data = json.load(fp) |
| 303 | + |
| 304 | + for _seed, rands in test_data.items(): |
| 305 | + seed = int(_seed) |
| 306 | + my_rands = sampel_seed_4(seed) |
| 307 | + for ri, my_r in enumerate(my_rands): |
| 308 | + r = rands[ri] |
| 309 | + # print("sample(%d) == %d should be %d" % (seed, my_r, r)) |
| 310 | + if my_r != r: |
| 311 | + raise Exception("sample_seed_1 test failed on\n sample(%d)[%d] == %d should be %d" % (seed, ri, my_r, r)) |
| 312 | + |
| 313 | + |
| 314 | +test_sampel_seed_4() |
0 commit comments