… | |
… | |
42 | rand_gen rndm; |
42 | rand_gen rndm; |
43 | |
43 | |
44 | void |
44 | void |
45 | tausworthe_random_generator::seed (uint32_t seed) |
45 | tausworthe_random_generator::seed (uint32_t seed) |
46 | { |
46 | { |
47 | state [0] = max ( 2U, seed * 69069U); |
47 | state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; |
48 | state [1] = max ( 8U, state [0] * 69069U); |
48 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
49 | state [2] = max ( 16U, state [1] * 69069U); |
49 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
50 | state [3] = max (128U, state [2] * 69069U); |
50 | state [3] = state [2] * 69069U; if (state [0] < 128) state [0] += 128U; |
51 | |
51 | |
52 | for (int i = 11; --i; ) |
52 | for (int i = 11; --i; ) |
53 | operator ()(); |
53 | operator ()(); |
54 | } |
54 | } |
55 | |
55 | |
… | |
… | |
63 | |
63 | |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
65 | } |
65 | } |
66 | |
66 | |
67 | uint32_t |
67 | uint32_t |
68 | tausworthe_random_generator::get_range (uint32_t r_max) |
68 | tausworthe_random_generator::get_range (uint32_t num) |
69 | { |
69 | { |
70 | return next () % r_max; |
70 | return (next () * (uint64_t)num) >> 32U; |
71 | } |
71 | } |
72 | |
72 | |
73 | // return a number within (min .. max) |
73 | // return a number within (min .. max) |
74 | int |
74 | int |
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
76 | { |
76 | { |
77 | return r_min + (*this) (max (r_max - r_min + 1, 1)); |
77 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
78 | } |
78 | } |
79 | |
79 | |
80 | /* |
80 | /* |
81 | * The random functions here take luck into account when rolling random |
81 | * The random functions here take luck into account when rolling random |
82 | * dice or numbers. This function has less of an impact the larger the |
82 | * dice or numbers. This function has less of an impact the larger the |