📄 random.cxx
字号:
#include "stdafx.h"
#include "random.h"/* * Title: random_number * Last Mod: Fri Mar 18 08:52:13 1988 * Author: Vincent Broman * <broman@schroeder.nosc.mil> */#ifdef __cplusplusextern "C" {#endif#define P 179#define PM1 (P - 1)#define Q (P - 10)#define STATE_SIZE 97#define MANTISSA_SIZE 24#define RANDOM_REALS 16777216.0#define INIT_C 362436.0#define INIT_CD 7654321.0#define INIT_CM 16777213.0static int dx[100] = {32422, 45287, 58516, 39741, 57138, 29827, 46592, 44601, 48254, 54751, 12844, 62709, 8842, 61691, 17688, 60273, 56662, 41431, 39620, 52653, 30946, 33907, 40752, 38825, 16686, 62671, 32348, 1381, 16954, 3819, 9288, 37601, 18438, 44743, 15604, 31773, 56978, 28771, 13408, 48409, 20958, 44991, 13964, 4565, 44522, 35035, 12152, 63057, 48822, 55223, 52004, 42637, 4162, 14419, 30096, 7817, 61070, 1711, 23228, 6725, 26010, 24267, 26280, 5569, 16742, 7335, 17748, 19709, 3570, 56387, 25280, 48121, 5950, 63903, 60140, 7861, 26954, 37051, 51672, 61745, 53270, 32151, 43908, 28525, 55202, 23603, 64496, 38249, 52206, 34959, 59164, 7973, 47354, 7851, 22792, 34977, 27334, 64135, 64948, 3549};static int dy[100] = {36584, 47361, 17318, 37351, 45460, 58429, 62514, 25987, 21248, 34617, 8574, 55007, 40748, 24053, 55178, 507, 33304, 58481, 57942, 49879, 42948, 22189, 52706, 46451, 31792, 45225, 58926, 13775, 11100, 44645, 14138, 24555, 41288, 52193, 36102, 4039, 35316, 17693, 29586, 57699, 20832, 5657, 14046, 12479, 9100, 64213, 58090, 6619, 60536, 28497, 17334, 30903, 22564, 44941, 58690, 59731, 53904, 46985, 5006, 51119, 34748, 17221, 55962, 12235, 25512, 52929, 1638, 64935, 4692, 38397, 8946, 52547, 65472, 38137, 31806, 64159, 22508, 34741, 7754, 41403, 1752, 59953, 54550, 40599, 47236, 63597, 11426, 36147, 55536, 44649, 28910, 51599, 37916, 51237, 44538, 28587, 54792, 49569, 44998, 23431};random_number::random_number(int x){ start_random_number(dx[x%100],dy[x%100]);}random_number::random_number(){ int x=0; start_random_number(dx[x%100],dy[x%100]);}unsigned int random_number::collapse (int anyint, unsigned int size)/* * return a value between 0 and size-1 inclusive. * this value will be anyint itself if possible, * otherwise another value in the required interval. */{ if (anyint < 0) anyint = - (anyint / 2); while (anyint >= size) anyint /= 2; return (anyint);}void random_number::start_random_number(int seed_a, int seed_b)/* * This procedure initialises the state table u for a lagged * Fibonacci sequence generator, filling it with random bits * from a small multiplicative congruential sequence. * The auxilliaries c, ni, and nj are also initialized. * The seeds are transformed into an initial state in such a way that * identical results are guaranteed across a wide variety of machines. */{ double s, bit; unsigned int ii, jj, kk, mm; unsigned int ll; unsigned int sd; unsigned int elt, bit_number; sd = collapse (seed_a, PM1 * PM1); ii = 1 + sd / PM1; jj = 1 + sd % PM1; sd = collapse (seed_b, PM1 * Q); kk = 1 + sd / PM1; ll = sd % Q; if (ii == 1 && jj == 1 && kk == 1) ii = 2; ni = STATE_SIZE - 1; nj = STATE_SIZE / 3; c = INIT_C; c /= RANDOM_REALS; /* compiler might mung the division itself */ cd = INIT_CD; cd /= RANDOM_REALS; cm = INIT_CM; cm /= RANDOM_REALS; for (elt = 0; elt < STATE_SIZE; elt += 1) { s = 0.0; bit = 1.0 / RANDOM_REALS; for (bit_number = 0; bit_number < MANTISSA_SIZE; bit_number += 1) { mm = (((ii * jj) % P) * kk) % P; ii = jj; jj = kk; kk = mm; ll = (53 * ll + 1) % Q; if (((ll * mm) % 64) >= 32) s += bit; bit += bit; } u[elt] = s; }} double random_number::next_random_number(void)/* * Return a uniformly distributed pseudo random number * in the range 0.0 .. 1.0-2**(-24) inclusive. * There are 2**24 possible return values. * Side-effects the non-local variables: u, c, ni, nj. */{double uni; if (u[ni] < u[nj]) uni = u[ni] + (1.0 - u[nj]); else uni = u[ni] - u[nj]; u[ni] = uni; if (ni > 0) ni -= 1; else ni = STATE_SIZE - 1; if (nj > 0) nj -= 1; else nj = STATE_SIZE - 1; if (c < cd) c = c + (cm - cd); else c = c - cd; if (uni < c) return (uni + (1.0 - c)); else return (uni - c);}#ifdef __cplusplus}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -