… | |
… | |
40 | |
40 | |
41 | #include <glib.h> |
41 | #include <glib.h> |
42 | |
42 | |
43 | refcnt_base::refcnt_t refcnt_dummy; |
43 | refcnt_base::refcnt_t refcnt_dummy; |
44 | ssize_t slice_alloc; |
44 | ssize_t slice_alloc; |
45 | rand_gen rndm, rmg_rndm; |
|
|
46 | |
45 | |
47 | #if !GCC_VERSION(3,4) |
46 | #if !GCC_VERSION(3,4) |
48 | int least_significant_bit (uint32_t x) |
47 | int least_significant_bit (uint32_t x) |
49 | { |
48 | { |
50 | x &= -x; // this isolates the lowest bit |
49 | x &= -x; // this isolates the lowest bit |
… | |
… | |
58 | if (x & 0xffff0000) r += 16; |
57 | if (x & 0xffff0000) r += 16; |
59 | |
58 | |
60 | return r; |
59 | return r; |
61 | } |
60 | } |
62 | #endif |
61 | #endif |
63 | |
|
|
64 | void |
|
|
65 | tausworthe_random_generator::seed (uint32_t seed) |
|
|
66 | { |
|
|
67 | state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U; |
|
|
68 | state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; |
|
|
69 | state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; |
|
|
70 | state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; |
|
|
71 | |
|
|
72 | for (int i = 11; --i; ) |
|
|
73 | next (); |
|
|
74 | } |
|
|
75 | |
|
|
76 | uint32_t |
|
|
77 | tausworthe_random_generator::next () |
|
|
78 | { |
|
|
79 | state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U); |
|
|
80 | state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U); |
|
|
81 | state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U); |
|
|
82 | state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); |
|
|
83 | |
|
|
84 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
|
|
85 | } |
|
|
86 | |
|
|
87 | template<class generator> |
|
|
88 | uint32_t |
|
|
89 | random_number_generator<generator>::get_range (uint32_t num) |
|
|
90 | { |
|
|
91 | return (this->next () * (uint64_t)num) >> 32U; |
|
|
92 | } |
|
|
93 | |
|
|
94 | // return a number within (min .. max) |
|
|
95 | template<class generator> |
|
|
96 | int |
|
|
97 | random_number_generator<generator>::get_range (int r_min, int r_max) |
|
|
98 | { |
|
|
99 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
|
|
100 | } |
|
|
101 | |
|
|
102 | template struct random_number_generator<tausworthe_random_generator>; |
|
|
103 | template struct random_number_generator<xorshift_random_generator>; |
|
|
104 | |
62 | |
105 | /******************************************************************************/ |
63 | /******************************************************************************/ |
106 | |
64 | |
107 | /* Checks a player-provided string which will become the msg property of |
65 | /* Checks a player-provided string which will become the msg property of |
108 | * an object for dangerous input. |
66 | * an object for dangerous input. |