|
| 1 | +DEF RS_RNG_NAME = u'sfmt' |
| 2 | +DEF SFMT_MEXP = 19937 |
| 3 | +DEF SFMT_N = 156 # (SFMT_MEXP / 128 + 1) |
| 4 | +DEF RS_SEED_NBYTES = 1 |
| 5 | +DEF RS_SEED_ARRAY_BITS = 32 |
| 6 | + |
| 7 | +ctypedef uint32_t rng_state_t |
| 8 | + |
| 9 | +cdef extern from "distributions.h": |
| 10 | + |
| 11 | + cdef union W128_T: |
| 12 | + uint32_t u[4] |
| 13 | + uint64_t u64[2] |
| 14 | + |
| 15 | + ctypedef W128_T w128_t; |
| 16 | + |
| 17 | + cdef struct SFMT_T: |
| 18 | + w128_t state[SFMT_N]; |
| 19 | + int idx; |
| 20 | + |
| 21 | + ctypedef SFMT_T sfmt_t; |
| 22 | + |
| 23 | + cdef struct s_aug_state: |
| 24 | + sfmt_t *rng |
| 25 | + binomial_t *binomial |
| 26 | + |
| 27 | + int has_gauss, shift_zig_random_int, has_uint32, has_gauss_float |
| 28 | + float gauss_float |
| 29 | + double gauss |
| 30 | + uint64_t zig_random_int |
| 31 | + uint32_t uinteger |
| 32 | + |
| 33 | + uint64_t *buffered_uint64 |
| 34 | + int buffer_loc |
| 35 | + |
| 36 | + ctypedef s_aug_state aug_state |
| 37 | + |
| 38 | + cdef void set_seed(aug_state* state, uint32_t seed) |
| 39 | + |
| 40 | + cdef void set_seed_by_array(aug_state* state, uint32_t init_key[], int key_length) |
| 41 | + |
| 42 | + cdef void jump_state(aug_state* state) |
| 43 | + |
| 44 | + |
| 45 | +ctypedef sfmt_t rng_t |
| 46 | + |
| 47 | +cdef object _get_state(aug_state state): |
| 48 | + cdef uint32_t [::1] key = np.zeros(4 * SFMT_N, dtype=np.uint32) |
| 49 | + cdef uint64_t [::1] buf = np.zeros(2 * SFMT_N, dtype=np.uint64) |
| 50 | + cdef Py_ssize_t i, j, key_loc = 0 |
| 51 | + cdef w128_t state_val |
| 52 | + for i in range(SFMT_N): |
| 53 | + state_val = state.rng.state[i] |
| 54 | + for j in range(4): |
| 55 | + key[key_loc] = state_val.u[j] |
| 56 | + key_loc += 1 |
| 57 | + for i in range(2 * SFMT_N): |
| 58 | + buf[i] = state.buffered_uint64[i] |
| 59 | + |
| 60 | + return (np.asarray(key), state.rng.idx, |
| 61 | + np.asarray(buf), state.buffer_loc) |
| 62 | + |
| 63 | +cdef object _set_state(aug_state *state, object state_info): |
| 64 | + cdef Py_ssize_t i, j, key_loc = 0 |
| 65 | + cdef uint32_t [::1] key = state_info[0] |
| 66 | + state.rng.idx = state_info[1] |
| 67 | + |
| 68 | + for i in range(SFMT_N): |
| 69 | + for j in range(4): |
| 70 | + state.rng.state[i].u[j] = key[key_loc] |
| 71 | + key_loc += 1 |
| 72 | + |
| 73 | + state.buffer_loc = <int>state_info[3] |
| 74 | + for i in range(2 * SFMT_N): |
| 75 | + state.buffered_uint64[i] = state_info[2][i] |
| 76 | + |
| 77 | + |
| 78 | +DEF CLASS_DOCSTRING = u""" |
| 79 | +RandomState(seed=None) |
| 80 | +
|
| 81 | +Container for the SIMD-based Mersenne Twister pseudo-random number generator. |
| 82 | +
|
| 83 | +``dSFMT.RandomState`` exposes a number of methods for generating random |
| 84 | +numbers drawn from a variety of probability distributions [1]_ . In addition |
| 85 | +to the distribution-specific arguments, each method takes a keyword argument |
| 86 | +`size` that defaults to ``None``. If `size` is ``None``, then a single |
| 87 | +value is generated and returned. If `size` is an integer, then a 1-D |
| 88 | +array filled with generated values is returned. If `size` is a tuple, |
| 89 | +then an array with that shape is filled and returned. |
| 90 | +
|
| 91 | +**No Compatibility Guarantee** |
| 92 | +
|
| 93 | +``dSFMT.RandomState`` does not make a guarantee that a fixed seed and a |
| 94 | +fixed series of calls to ``dSFMT.RandomState`` methods using the same |
| 95 | +parameters will always produce the same results. This is different from |
| 96 | +``numpy.random.RandomState`` guarantee. This is done to simplify improving |
| 97 | +random number generators. To ensure identical results, you must use the |
| 98 | +same release version. |
| 99 | +
|
| 100 | +Parameters |
| 101 | +---------- |
| 102 | +seed : {None, int, array_like}, optional |
| 103 | + Random seed initializing the pseudo-random number generator. |
| 104 | + Can be an integer in [0, 2**32-1], array of integers in |
| 105 | + [0, 2**32-1] or ``None`` (the default). If `seed` is ``None``, |
| 106 | + then ``dSFMT.RandomState`` will try to read entropy from |
| 107 | + ``/dev/urandom`` (or the Windows analog) if available to |
| 108 | + produce a 64-bit seed. If unavailable, the a 64-bit hash of the time |
| 109 | + and process ID is used. |
| 110 | +
|
| 111 | +Notes |
| 112 | +----- |
| 113 | +The Python stdlib module "random" also contains a Mersenne Twister |
| 114 | +pseudo-random number generator with a number of methods that are similar |
| 115 | +to the ones available in ``RandomState``. The `RandomState` object, besides |
| 116 | +being NumPy-aware, also has the advantage that it provides a much larger |
| 117 | +number of probability distributions to choose from. |
| 118 | +
|
| 119 | +>>> from randomstate.entropy import random_entropy |
| 120 | +>>> import randomstate.prng.sfmt as rnd |
| 121 | +>>> seed = random_entropy() |
| 122 | +>>> rs = [rnd.RandomState(seed) for _ in range(10)] |
| 123 | +
|
| 124 | +**State and Seeding** |
| 125 | +
|
| 126 | +The ``sfmt.RandomState`` state vector consists of a 624 element array of |
| 127 | +32-bit unsigned integers plus a single integer value between 0 and 624 |
| 128 | +indicating the current position within the main array. The implementation |
| 129 | +used here augments this with a array of 64-bit unsigned integers which are used |
| 130 | +to efficiently access the random numbers produced by the SFMT generator. |
| 131 | +
|
| 132 | +``sfmt.RandomState`` is seeded using either a single 32-bit unsigned integer |
| 133 | +or a vector of 32-bit unsigned integers. In either case, the input seed is |
| 134 | +used as an input (or inputs) for a hashing function, and the output of the |
| 135 | +hashing function is used as the initial state. Using a single 32-bit value |
| 136 | +for the seed can only initialize a small range of the possible initial |
| 137 | +state values. |
| 138 | +
|
| 139 | +.. [1] Mutsuo Saito and Makoto Matsumoto, "SIMD-oriented Fast Mersenne |
| 140 | + Twister: a 128-bit Pseudorandom Number Generator." Monte Carlo |
| 141 | + and Quasi-Monte Carlo Methods 2006, Springer, pp. 607 -- 622, 2008. |
| 142 | +""" |
0 commit comments