From 00bf69fb47679fe50c8b771a249f33fce4b96aaf Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Tue, 13 Mar 2018 12:25:07 +0000 Subject: [PATCH] ENH: Switch to int64 Switch to int64 to avoid platform-dependencies for integer values randoms --- _randomgen/README.md | 1 + _randomgen/TODO.md | 27 ---- _randomgen/core_prng/common.pxd | 14 +- _randomgen/core_prng/common.pyx | 102 ++++++------- _randomgen/core_prng/distributions.pxd | 30 ++-- _randomgen/core_prng/generator.pyx | 54 ++++--- .../src/distributions/distributions.c | 134 ++++++++---------- .../src/distributions/distributions.h | 45 ++++-- .../core_prng/tests/test_numpy_mt19937.py | 9 +- _randomgen/demo.py | 103 -------------- 10 files changed, 196 insertions(+), 323 deletions(-) delete mode 100644 _randomgen/TODO.md delete mode 100644 _randomgen/demo.py diff --git a/_randomgen/README.md b/_randomgen/README.md index 213a6cf4b1f6..19f216b3eaf4 100644 --- a/_randomgen/README.md +++ b/_randomgen/README.md @@ -96,6 +96,7 @@ The RNGs include: the optional keyword argument `dtype` * Core random number generators can fill existing arrays using the `out` keyword argument +* Standardizes integer-values random values as int64 for all platforms. ### New Functions diff --git a/_randomgen/TODO.md b/_randomgen/TODO.md deleted file mode 100644 index ed2697d9e826..000000000000 --- a/_randomgen/TODO.md +++ /dev/null @@ -1,27 +0,0 @@ -# TODO -13. Simplify state - -## Done -1. Add PCG64 -2. Add dSFMT -3. Add xorshift2014 -4. Augment state to include has_gauss and gauss -5. Augment state to have binomial structure -6. Port over 0 parameter distributions - * standard normal ziggurat - * standard normal ziggurat float - * standard exponential ziggurat - * standard exponential ziggurat float - * standard exponential float - * standard normal - * standard normal float - * standard gamma - Not implement: This is a 1 param - * standard gamma float - Not implement: This is a 1 param -7. Remove SplitMix64 as an external generator -8. Restore ability to use `out` in core distributions -9. Add correct carry for ThreeFry to allow full set of counters. Important when implemeting jump -10. Seed/Inc for PCG64 -11. Advance/Jump for PCG64 -12. Key/Counter for ThreeFry -0. **NOT IMPLEMENTABLE** due to limits on inheritance in Cython: Use inheritance to simplify CorePRNG structure. The natural base is - xoroshiro128. diff --git a/_randomgen/core_prng/common.pxd b/_randomgen/core_prng/common.pxd index c571c57a2950..66d961edc4ef 100644 --- a/_randomgen/core_prng/common.pxd +++ b/_randomgen/core_prng/common.pxd @@ -38,12 +38,12 @@ ctypedef double (*random_double_3)(prng_t *state, double a, double b, double c) ctypedef float (*random_float_0)(prng_t *state) nogil ctypedef float (*random_float_1)(prng_t *state, float a) nogil -ctypedef long (*random_uint_0)(prng_t *state) nogil -ctypedef long (*random_uint_d)(prng_t *state, double a) nogil -ctypedef long (*random_uint_dd)(prng_t *state, double a, double b) nogil -ctypedef long (*random_uint_di)(prng_t *state, double a, uint64_t b) nogil -ctypedef long (*random_uint_i)(prng_t *state, long a) nogil -ctypedef long (*random_uint_iii)(prng_t *state, long a, long b, long c) nogil +ctypedef int64_t (*random_uint_0)(prng_t *state) nogil +ctypedef int64_t (*random_uint_d)(prng_t *state, double a) nogil +ctypedef int64_t (*random_uint_dd)(prng_t *state, double a, double b) nogil +ctypedef int64_t (*random_uint_di)(prng_t *state, double a, uint64_t b) nogil +ctypedef int64_t (*random_uint_i)(prng_t *state, int64_t a) nogil +ctypedef int64_t (*random_uint_iii)(prng_t *state, int64_t a, int64_t b, int64_t c) nogil ctypedef uint32_t (*random_uint_0_32)(prng_t *state) nogil ctypedef uint32_t (*random_uint_1_i_32)(prng_t *state, uint32_t a) nogil @@ -71,7 +71,7 @@ cdef object cont(void *func, prng_t *state, object size, object lock, int narg, object out) cdef object disc(void *func, prng_t *state, object size, object lock, - int narg_double, int narg_long, + int narg_double, int narg_int64, object a, object a_name, constraint_type a_constraint, object b, object b_name, constraint_type b_constraint, object c, object c_name, constraint_type c_constraint) diff --git a/_randomgen/core_prng/common.pyx b/_randomgen/core_prng/common.pyx index fb3e96697bae..b25510e1a955 100644 --- a/_randomgen/core_prng/common.pyx +++ b/_randomgen/core_prng/common.pyx @@ -407,7 +407,7 @@ cdef object discrete_broadcast_d(void *func, prng_t *state, object size, object np.ndarray a_arr, object a_name, constraint_type a_constraint): cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data cdef np.broadcast it cdef random_uint_d f = (func) cdef np.npy_intp i, n @@ -416,12 +416,12 @@ cdef object discrete_broadcast_d(void *func, prng_t *state, object size, object check_array_constraint(a_arr, a_name, a_constraint) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: #randoms = np.empty(np.shape(a_arr), np.double) - randoms = np.PyArray_SimpleNew(np.PyArray_NDIM(a_arr), np.PyArray_DIMS(a_arr), np.NPY_LONG) + randoms = np.PyArray_SimpleNew(np.PyArray_NDIM(a_arr), np.PyArray_DIMS(a_arr), np.NPY_INT64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew2(randoms, a_arr) @@ -438,7 +438,7 @@ cdef object discrete_broadcast_dd(void *func, prng_t *state, object size, object np.ndarray a_arr, object a_name, constraint_type a_constraint, np.ndarray b_arr, object b_name, constraint_type b_constraint): cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data cdef np.broadcast it cdef random_uint_dd f = (func) cdef np.npy_intp i, n @@ -449,13 +449,13 @@ cdef object discrete_broadcast_dd(void *func, prng_t *state, object size, object check_array_constraint(b_arr, b_name, b_constraint) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: it = np.PyArray_MultiIterNew2(a_arr, b_arr) - randoms = np.empty(it.shape, np.int) - # randoms = np.PyArray_SimpleNew(it.nd, np.PyArray_DIMS(it), np.NPY_LONG) + randoms = np.empty(it.shape, np.int64) + # randoms = np.PyArray_SimpleNew(it.nd, np.PyArray_DIMS(it), np.NPY_INT64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew3(randoms, a_arr, b_arr) @@ -473,7 +473,7 @@ cdef object discrete_broadcast_di(void *func, prng_t *state, object size, object np.ndarray a_arr, object a_name, constraint_type a_constraint, np.ndarray b_arr, object b_name, constraint_type b_constraint): cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data cdef np.broadcast it cdef random_uint_di f = (func) cdef np.npy_intp i, n @@ -486,20 +486,20 @@ cdef object discrete_broadcast_di(void *func, prng_t *state, object size, object check_array_constraint(b_arr, b_name, b_constraint) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: it = np.PyArray_MultiIterNew2(a_arr, b_arr) - randoms = np.empty(it.shape, np.int) + randoms = np.empty(it.shape, np.int64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew3(randoms, a_arr, b_arr) with lock, nogil: for i in range(n): a_val = (np.PyArray_MultiIter_DATA(it, 1))[0] - b_val = (np.PyArray_MultiIter_DATA(it, 2))[0] - (np.PyArray_MultiIter_DATA(it, 0))[0] = f(state, a_val, b_val) + b_val = (np.PyArray_MultiIter_DATA(it, 2))[0] + (np.PyArray_MultiIter_DATA(it, 0))[0] = f(state, a_val, b_val) np.PyArray_MultiIter_NEXT(it) @@ -510,7 +510,7 @@ cdef object discrete_broadcast_iii(void *func, prng_t *state, object size, objec np.ndarray b_arr, object b_name, constraint_type b_constraint, np.ndarray c_arr, object c_name, constraint_type c_constraint): cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data cdef np.broadcast it cdef random_uint_iii f = (func) cdef np.npy_intp i, n @@ -525,20 +525,20 @@ cdef object discrete_broadcast_iii(void *func, prng_t *state, object size, objec check_array_constraint(c_arr, c_name, c_constraint) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: it = np.PyArray_MultiIterNew3(a_arr, b_arr, c_arr) - randoms = np.empty(it.shape, np.int) + randoms = np.empty(it.shape, np.int64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew4(randoms, a_arr, b_arr, c_arr) with lock, nogil: for i in range(n): - a_val = (np.PyArray_MultiIter_DATA(it, 1))[0] - b_val = (np.PyArray_MultiIter_DATA(it, 2))[0] - c_val = (np.PyArray_MultiIter_DATA(it, 3))[0] + a_val = (np.PyArray_MultiIter_DATA(it, 1))[0] + b_val = (np.PyArray_MultiIter_DATA(it, 2))[0] + c_val = (np.PyArray_MultiIter_DATA(it, 3))[0] randoms_data[i] = f(state, a_val, b_val, c_val) np.PyArray_MultiIter_NEXT(it) @@ -548,7 +548,7 @@ cdef object discrete_broadcast_iii(void *func, prng_t *state, object size, objec cdef object discrete_broadcast_i(void *func, prng_t *state, object size, object lock, np.ndarray a_arr, object a_name, constraint_type a_constraint): cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data cdef np.broadcast it cdef random_uint_i f = (func) cdef np.npy_intp i, n @@ -557,32 +557,32 @@ cdef object discrete_broadcast_i(void *func, prng_t *state, object size, object check_array_constraint(a_arr, a_name, a_constraint) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: - randoms = np.PyArray_SimpleNew(np.PyArray_NDIM(a_arr), np.PyArray_DIMS(a_arr), np.NPY_LONG) + randoms = np.PyArray_SimpleNew(np.PyArray_NDIM(a_arr), np.PyArray_DIMS(a_arr), np.NPY_INT64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew2(randoms, a_arr) with lock, nogil: for i in range(n): - a_val = (np.PyArray_MultiIter_DATA(it, 1))[0] + a_val = (np.PyArray_MultiIter_DATA(it, 1))[0] randoms_data[i] = f(state, a_val) np.PyArray_MultiIter_NEXT(it) return randoms -# Needs double , double-double , double-long, long , long-long-long +# Needs double , double-double , double-int64_t, int64_t , int64_t-int64_t-int64_t cdef object disc(void *func, prng_t *state, object size, object lock, - int narg_double, int narg_long, + int narg_double, int narg_int64, object a, object a_name, constraint_type a_constraint, object b, object b_name, constraint_type b_constraint, object c, object c_name, constraint_type c_constraint): cdef double _da = 0, _db = 0 - cdef long _ia = 0, _ib = 0 , _ic = 0 + cdef int64_t _ia = 0, _ib = 0 , _ic = 0 cdef bint is_scalar = True if narg_double > 0: a_arr = np.PyArray_FROM_OTF(a, np.NPY_DOUBLE, np.NPY_ALIGNED) @@ -590,22 +590,22 @@ cdef object disc(void *func, prng_t *state, object size, object lock, if narg_double > 1: b_arr = np.PyArray_FROM_OTF(b, np.NPY_DOUBLE, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(b_arr) == 0 - elif narg_long == 1: - b_arr = np.PyArray_FROM_OTF(b, np.NPY_LONG, np.NPY_ALIGNED) + elif narg_int64 == 1: + b_arr = np.PyArray_FROM_OTF(b, np.NPY_INT64, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(b_arr) == 0 else: - if narg_long > 0: - a_arr = np.PyArray_FROM_OTF(a, np.NPY_LONG, np.NPY_ALIGNED) + if narg_int64 > 0: + a_arr = np.PyArray_FROM_OTF(a, np.NPY_INT64, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(a_arr) == 0 - if narg_long > 1: - b_arr = np.PyArray_FROM_OTF(b, np.NPY_LONG, np.NPY_ALIGNED) + if narg_int64 > 1: + b_arr = np.PyArray_FROM_OTF(b, np.NPY_INT64, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(b_arr) == 0 - if narg_long > 2 : - c_arr = np.PyArray_FROM_OTF(c, np.NPY_LONG, np.NPY_ALIGNED) + if narg_int64 > 2 : + c_arr = np.PyArray_FROM_OTF(c, np.NPY_INT64, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(c_arr) == 0 if not is_scalar: - if narg_long == 0: + if narg_int64 == 0: if narg_double == 1: return discrete_broadcast_d(func, state, size, lock, a_arr, a_name, a_constraint) @@ -613,7 +613,7 @@ cdef object disc(void *func, prng_t *state, object size, object lock, return discrete_broadcast_dd(func, state, size, lock, a_arr, a_name, a_constraint, b_arr, b_name, b_constraint) - elif narg_long == 1: + elif narg_int64 == 1: if narg_double == 0: return discrete_broadcast_i(func, state, size, lock, a_arr, a_name, a_constraint) @@ -634,34 +634,34 @@ cdef object disc(void *func, prng_t *state, object size, object lock, _db = PyFloat_AsDouble(b) if b_constraint != CONS_NONE and is_scalar: check_constraint(_db, b_name, b_constraint) - elif narg_long == 1: + elif narg_int64 == 1: _ib = PyInt_AsLong(b) if b_constraint != CONS_NONE and is_scalar: check_constraint(_ib, b_name, b_constraint) else: - if narg_long > 0: + if narg_int64 > 0: _ia = PyInt_AsLong(a) if a_constraint != CONS_NONE and is_scalar: check_constraint(_ia, a_name, a_constraint) - if narg_long > 1: + if narg_int64 > 1: _ib = PyInt_AsLong(b) if b_constraint != CONS_NONE and is_scalar: check_constraint(_ib, b_name, b_constraint) - if narg_long > 2 : + if narg_int64 > 2 : _ic = PyInt_AsLong(c) if c_constraint != CONS_NONE and is_scalar: check_constraint(_ic, c_name, c_constraint) if size is None: with lock: - if narg_long == 0: + if narg_int64 == 0: if narg_double == 0: return (func)(state) elif narg_double == 1: return (func)(state, _da) elif narg_double == 2: return (func)(state, _da, _db) - elif narg_long == 1: + elif narg_int64 == 1: if narg_double == 0: return (func)(state, _ia) if narg_double == 1: @@ -670,8 +670,8 @@ cdef object disc(void *func, prng_t *state, object size, object lock, return (func)(state, _ia, _ib, _ic) cdef np.npy_intp i, n - cdef np.ndarray randoms = np.empty(size, np.int) - cdef np.int_t *randoms_data + cdef np.ndarray randoms = np.empty(size, np.int64) + cdef np.int64_t *randoms_data cdef random_uint_0 f0; cdef random_uint_d fd; cdef random_uint_dd fdd; @@ -680,10 +680,10 @@ cdef object disc(void *func, prng_t *state, object size, object lock, cdef random_uint_iii fiii; n = np.PyArray_SIZE(randoms) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) with lock, nogil: - if narg_long == 0: + if narg_int64 == 0: if narg_double == 0: f0 = (func) for i in range(n): @@ -696,7 +696,7 @@ cdef object disc(void *func, prng_t *state, object size, object lock, fdd = (func) for i in range(n): randoms_data[i] = fdd(state, _da, _db) - elif narg_long == 1: + elif narg_int64 == 1: if narg_double == 0: fi = (func) for i in range(n): diff --git a/_randomgen/core_prng/distributions.pxd b/_randomgen/core_prng/distributions.pxd index 29dc5f114fa3..44f3520690ef 100644 --- a/_randomgen/core_prng/distributions.pxd +++ b/_randomgen/core_prng/distributions.pxd @@ -8,11 +8,11 @@ cdef extern from "src/distributions/distributions.h": struct s_binomial_t: int has_binomial double psave - long nsave + int64_t nsave double r double q double fm - long m + int64_t m double p1 double xm double xl @@ -50,8 +50,8 @@ cdef extern from "src/distributions/distributions.h": int64_t random_positive_int64(prng_t *prng_state) nogil int32_t random_positive_int32(prng_t *prng_state) nogil - long random_positive_int(prng_t *prng_state) nogil - unsigned long random_uint(prng_t *prng_state) nogil + int64_t random_positive_int(prng_t *prng_state) nogil + uint64_t random_uint(prng_t *prng_state) nogil double random_normal_zig(prng_t *prng_state, double loc, double scale) nogil @@ -82,17 +82,17 @@ cdef extern from "src/distributions/distributions.h": double random_triangular(prng_t *prng_state, double left, double mode, double right) nogil - long random_poisson(prng_t *prng_state, double lam) nogil - long random_negative_binomial(prng_t *prng_state, double n, double p) nogil - long random_binomial(prng_t *prng_state, double p, long n, binomial_t *binomial) nogil - long random_logseries(prng_t *prng_state, double p) nogil - long random_geometric_search(prng_t *prng_state, double p) nogil - long random_geometric_inversion(prng_t *prng_state, double p) nogil - long random_geometric(prng_t *prng_state, double p) nogil - long random_zipf(prng_t *prng_state, double a) nogil - long random_hypergeometric(prng_t *prng_state, long good, long bad, - long sample) nogil - unsigned long random_interval(prng_t *prng_state, unsigned long max) nogil + int64_t random_poisson(prng_t *prng_state, double lam) nogil + int64_t random_negative_binomial(prng_t *prng_state, double n, double p) nogil + int64_t random_binomial(prng_t *prng_state, double p, int64_t n, binomial_t *binomial) nogil + int64_t random_logseries(prng_t *prng_state, double p) nogil + int64_t random_geometric_search(prng_t *prng_state, double p) nogil + int64_t random_geometric_inversion(prng_t *prng_state, double p) nogil + int64_t random_geometric(prng_t *prng_state, double p) nogil + int64_t random_zipf(prng_t *prng_state, double a) nogil + int64_t random_hypergeometric(prng_t *prng_state, int64_t good, int64_t bad, + int64_t sample) nogil + uint64_t random_interval(prng_t *prng_state, uint64_t max) nogil uint64_t random_bounded_uint64(prng_t *prng_state, uint64_t off, uint64_t rng, uint64_t mask) nogil uint32_t random_buffered_bounded_uint32(prng_t *prng_state, uint32_t off, diff --git a/_randomgen/core_prng/generator.pyx b/_randomgen/core_prng/generator.pyx index 51cdebc19550..ec52a1839f0b 100644 --- a/_randomgen/core_prng/generator.pyx +++ b/_randomgen/core_prng/generator.pyx @@ -487,14 +487,14 @@ cdef class RandomGenerator: """ cdef np.npy_intp n cdef np.ndarray randoms - cdef long *randoms_data + cdef int64_t *randoms_data if size is None: with self.lock: return random_positive_int(self._prng) - randoms = np.empty(size, dtype=np.int) - randoms_data = np.PyArray_DATA(randoms) + randoms = np.empty(size, dtype=np.int64) + randoms_data = np.PyArray_DATA(randoms) n = np.PyArray_SIZE(randoms) for i in range(n): @@ -776,7 +776,7 @@ cdef class RandomGenerator: raise ValueError("Fewer non-zero entries in p than size") n_uniq = 0 p = p.copy() - found = np.zeros(shape, dtype=np.int) + found = np.zeros(shape, dtype=np.int64) flat_found = found.ravel() while n_uniq < size: x = self.rand(size - n_uniq) @@ -1039,13 +1039,11 @@ cdef class RandomGenerator: """ random_integers(low, high=None, size=None) - Random integers of type np.int between `low` and `high`, inclusive. + Random integers of type np.int64 between `low` and `high`, inclusive. - Return random integers of type np.int from the "discrete uniform" + Return random integers of type np.int64 from the "discrete uniform" distribution in the closed interval [`low`, `high`]. If `high` is - None (the default), then results are from [1, `low`]. The np.int - type translates to the C long type used by Python 2 for "short" - integers and its precision is platform dependent. + None (the default), then results are from [1, `low`]. This function has been deprecated. Use randint instead. @@ -1418,7 +1416,7 @@ cdef class RandomGenerator: it = np.PyArray_MultiIterNew5(randoms, oloc, v_real, v_imag, rho) with self.lock, nogil: - n2 = 2 * n # Avoid compiler noise for cast to long + n2 = 2 * n # Avoid compiler noise for cast for i in range(n2): randoms_data[i] = random_gauss_zig(self._prng) with nogil: @@ -3166,36 +3164,36 @@ cdef class RandomGenerator: # Uses a custom implementation since self._binomial is required cdef double _dp = 0 - cdef long _in = 0 + cdef int64_t _in = 0 cdef bint is_scalar = True cdef np.npy_intp i, cnt cdef np.ndarray randoms - cdef np.int_t *randoms_data + cdef np.int64_t *randoms_data cdef np.broadcast it p_arr = np.PyArray_FROM_OTF(p, np.NPY_DOUBLE, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(p_arr) == 0 - n_arr = np.PyArray_FROM_OTF(n, np.NPY_LONG, np.NPY_ALIGNED) + n_arr = np.PyArray_FROM_OTF(n, np.NPY_INT64, np.NPY_ALIGNED) is_scalar = is_scalar and np.PyArray_NDIM(n_arr) == 0 if not is_scalar: check_array_constraint(p_arr, 'p', CONS_BOUNDED_0_1_NOTNAN) check_array_constraint(n_arr, 'n', CONS_NON_NEGATIVE) if size is not None: - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) else: it = np.PyArray_MultiIterNew2(p_arr, n_arr) - randoms = np.empty(it.shape, np.int) + randoms = np.empty(it.shape, np.int64) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) cnt = np.PyArray_SIZE(randoms) it = np.PyArray_MultiIterNew3(randoms, p_arr, n_arr) with self.lock, nogil: for i in range(cnt): _dp = (np.PyArray_MultiIter_DATA(it, 1))[0] - _in = (np.PyArray_MultiIter_DATA(it, 2))[0] - (np.PyArray_MultiIter_DATA(it, 0))[0] = random_binomial(self._prng, _dp, _in, self._binomial) + _in = (np.PyArray_MultiIter_DATA(it, 2))[0] + (np.PyArray_MultiIter_DATA(it, 0))[0] = random_binomial(self._prng, _dp, _in, self._binomial) np.PyArray_MultiIter_NEXT(it) @@ -3210,9 +3208,9 @@ cdef class RandomGenerator: with self.lock: return random_binomial(self._prng, _dp, _in, self._binomial) - randoms = np.empty(size, np.int) + randoms = np.empty(size, np.int64) cnt = np.PyArray_SIZE(randoms) - randoms_data = np.PyArray_DATA(randoms) + randoms_data = np.PyArray_DATA(randoms) with self.lock, nogil: for i in range(cnt): @@ -3334,7 +3332,7 @@ cdef class RandomGenerator: :math:`k` events occurring within the observed interval :math:`\\lambda`. - Because the output is limited to the range of the C long type, a + Because the output is limited to the range of the C int64 type, a ValueError is raised when `lam` is within 10 sigma of the maximum representable value. @@ -3589,11 +3587,11 @@ cdef class RandomGenerator: """ cdef bint is_scalar = True cdef np.ndarray ongood, onbad, onsample - cdef long lngood, lnbad, lnsample + cdef int64_t lngood, lnbad, lnsample - ongood = np.PyArray_FROM_OTF(ngood, np.NPY_LONG, np.NPY_ALIGNED) - onbad = np.PyArray_FROM_OTF(nbad, np.NPY_LONG, np.NPY_ALIGNED) - onsample = np.PyArray_FROM_OTF(nsample, np.NPY_LONG, np.NPY_ALIGNED) + ongood = np.PyArray_FROM_OTF(ngood, np.NPY_INT64, np.NPY_ALIGNED) + onbad = np.PyArray_FROM_OTF(nbad, np.NPY_INT64, np.NPY_ALIGNED) + onsample = np.PyArray_FROM_OTF(nsample, np.NPY_INT64, np.NPY_ALIGNED) if np.PyArray_NDIM(ongood) == np.PyArray_NDIM(onbad) == np.PyArray_NDIM(onsample) == 0: @@ -3941,7 +3939,7 @@ cdef class RandomGenerator: cdef np.npy_intp d, i, j, dn, sz cdef np.ndarray parr "arrayObject_parr", mnarr "arrayObject_mnarr" cdef double *pix - cdef long *mnix + cdef int64_t *mnix cdef double Sum d = len(pvals) @@ -3959,9 +3957,9 @@ cdef class RandomGenerator: except: shape = tuple(size) + (d,) - multin = np.zeros(shape, dtype=np.int) + multin = np.zeros(shape, dtype=np.int64) mnarr = multin - mnix = np.PyArray_DATA(mnarr) + mnix = np.PyArray_DATA(mnarr) sz = np.PyArray_SIZE(mnarr) with self.lock, nogil: diff --git a/_randomgen/core_prng/src/distributions/distributions.c b/_randomgen/core_prng/src/distributions/distributions.c index 3dd83e66d38a..daed964ecc09 100644 --- a/_randomgen/core_prng/src/distributions/distributions.c +++ b/_randomgen/core_prng/src/distributions/distributions.c @@ -3,13 +3,9 @@ #include "ziggurat_constants.h" /* Random generators for external use */ -float random_sample_f(prng_t *prng_state) { - return random_float(prng_state); -} +float random_sample_f(prng_t *prng_state) { return random_float(prng_state); } -double random_sample(prng_t *prng_state) { - return random_double(prng_state); -} +double random_sample(prng_t *prng_state) { return random_double(prng_state); } double random_standard_exponential(prng_t *prng_state) { return -log(1.0 - random_double(prng_state)); @@ -75,8 +71,7 @@ static double standard_exponential_zig_unlikely(prng_t *prng_state, uint8_t idx, double x) { if (idx == 0) { return ziggurat_exp_r - log(random_double(prng_state)); - } else if ((fe_double[idx - 1] - fe_double[idx]) * - random_double(prng_state) + + } else if ((fe_double[idx - 1] - fe_double[idx]) * random_double(prng_state) + fe_double[idx] < exp(-x)) { return x; @@ -158,16 +153,14 @@ double random_gauss_zig(prng_t *prng_state) { return x; // # 99.3% of the time return here if (idx == 0) { for (;;) { - xx = -ziggurat_nor_inv_r * - log(random_double(prng_state)); + xx = -ziggurat_nor_inv_r * log(random_double(prng_state)); yy = -log(random_double(prng_state)); if (yy + yy > xx * xx) return ((rabs >> 8) & 0x1) ? -(ziggurat_nor_r + xx) : ziggurat_nor_r + xx; } } else { - if (((fi_double[idx - 1] - fi_double[idx]) * - random_double(prng_state) + + if (((fi_double[idx - 1] - fi_double[idx]) * random_double(prng_state) + fi_double[idx]) < exp(-0.5 * x * x)) return x; } @@ -402,15 +395,15 @@ int32_t random_positive_int32(prng_t *prng_state) { return random_uint32(prng_state) >> 1; } -long random_positive_int(prng_t *prng_state) { +int64_t random_positive_int(prng_t *prng_state) { #if ULONG_MAX <= 0xffffffffUL - return (long)(random_uint32(prng_state) >> 1); + return (int64_t)(random_uint32(prng_state) >> 1); #else - return (long)(random_uint64(prng_state) >> 1); + return (int64_t)(random_uint64(prng_state) >> 1); #endif } -unsigned long random_uint(prng_t *prng_state) { +uint64_t random_uint(prng_t *prng_state) { #if ULONG_MAX <= 0xffffffffUL return random_uint32(prng_state); #else @@ -425,7 +418,7 @@ unsigned long random_uint(prng_t *prng_state) { */ static double loggam(double x) { double x0, x2, xp, gl, gl0; - long k, n; + int64_t k, n; static double a[10] = {8.333333333333333e-02, -2.777777777777778e-03, 7.936507936507937e-04, -5.952380952380952e-04, @@ -437,7 +430,7 @@ static double loggam(double x) { if ((x == 1.0) || (x == 2.0)) { return 0.0; } else if (x <= 7.0) { - n = (long)(7 - x); + n = (int64_t)(7 - x); x0 = x + n; } x2 = 1.0 / (x0 * x0); @@ -584,8 +577,8 @@ double random_standard_t(prng_t *prng_state, double df) { return sqrt(df / 2) * num / sqrt(denom); } -static long random_poisson_mult(prng_t *prng_state, double lam) { - long X; +static int64_t random_poisson_mult(prng_t *prng_state, double lam) { + int64_t X; double prod, U, enlam; enlam = exp(-lam); @@ -609,8 +602,8 @@ static long random_poisson_mult(prng_t *prng_state, double lam) { */ #define LS2PI 0.91893853320467267 #define TWELFTH 0.083333333333333333333333 -static long random_poisson_ptrs(prng_t *prng_state, double lam) { - long k; +static int64_t random_poisson_ptrs(prng_t *prng_state, double lam) { + int64_t k; double U, V, slam, loglam, a, b, invalpha, vr, us; slam = sqrt(lam); @@ -624,7 +617,7 @@ static long random_poisson_ptrs(prng_t *prng_state, double lam) { U = random_sample(prng_state) - 0.5; V = random_sample(prng_state); us = 0.5 - fabs(U); - k = (long)floor((2 * a / us + b) * U + lam + 0.43); + k = (int64_t)floor((2 * a / us + b) * U + lam + 0.43); if ((us >= 0.07) && (V <= vr)) { return k; } @@ -638,7 +631,7 @@ static long random_poisson_ptrs(prng_t *prng_state, double lam) { } } -long random_poisson(prng_t *prng_state, double lam) { +int64_t random_poisson(prng_t *prng_state, double lam) { if (lam >= 10) { return random_poisson_ptrs(prng_state, lam); } else if (lam == 0) { @@ -648,18 +641,18 @@ long random_poisson(prng_t *prng_state, double lam) { } } -long random_negative_binomial(prng_t *prng_state, double n, double p) { +int64_t random_negative_binomial(prng_t *prng_state, double n, double p) { double Y = random_gamma(prng_state, n, (1 - p) / p); return random_poisson(prng_state, Y); } -long random_binomial_btpe(prng_t *prng_state, long n, double p, binomial_t* binomial) { +int64_t random_binomial_btpe(prng_t *prng_state, int64_t n, double p, + binomial_t *binomial) { double r, q, fm, p1, xm, xl, xr, c, laml, lamr, p2, p3, p4; double a, u, v, s, F, rho, t, A, nrq, x1, x2, f1, f2, z, z2, w, w2, x; - long m, y, k, i; + int64_t m, y, k, i; - if (!(binomial->has_binomial) || - (binomial->nsave != n) || + if (!(binomial->has_binomial) || (binomial->nsave != n) || (binomial->psave != p)) { /* initialize */ binomial->nsave = n; @@ -668,9 +661,8 @@ long random_binomial_btpe(prng_t *prng_state, long n, double p, binomial_t* bino binomial->r = r = min(p, 1.0 - p); binomial->q = q = 1.0 - r; binomial->fm = fm = n * r + r; - binomial->m = m = (long)floor(binomial->fm); - binomial->p1 = p1 = - floor(2.195 * sqrt(n * r * q) - 4.6 * q) + 0.5; + binomial->m = m = (int64_t)floor(binomial->fm); + binomial->p1 = p1 = floor(2.195 * sqrt(n * r * q) - 4.6 * q) + 0.5; binomial->xm = xm = m + 0.5; binomial->xl = xl = xm - p1; binomial->xr = xr = xm + p1; @@ -706,7 +698,7 @@ long random_binomial_btpe(prng_t *prng_state, long n, double p, binomial_t* bino v = random_sample(prng_state); if (u > p1) goto Step20; - y = (long)floor(xm - p1 * v + u); + y = (int64_t)floor(xm - p1 * v + u); goto Step60; Step20: @@ -716,26 +708,26 @@ long random_binomial_btpe(prng_t *prng_state, long n, double p, binomial_t* bino v = v * c + 1.0 - fabs(m - x + 0.5) / p1; if (v > 1.0) goto Step10; - y = (long)floor(x); + y = (int64_t)floor(x); goto Step50; Step30: if (u > p3) goto Step40; - y = (long)floor(xl + log(v) / laml); + y = (int64_t)floor(xl + log(v) / laml); if (y < 0) goto Step10; v = v * (u - p2) * laml; goto Step50; Step40: - y = (long)floor(xr - log(v) / lamr); + y = (int64_t)floor(xr - log(v) / lamr); if (y > n) goto Step10; v = v * (u - p3) * lamr; Step50: - k = labs(y - m); + k = llabs(y - m); if ((k > 20) && (k < ((nrq) / 2.0 - 1))) goto Step52; @@ -794,12 +786,12 @@ long random_binomial_btpe(prng_t *prng_state, long n, double p, binomial_t* bino return y; } -long random_binomial_inversion(prng_t *prng_state, long n, double p, binomial_t* binomial) { +int64_t random_binomial_inversion(prng_t *prng_state, int64_t n, double p, + binomial_t *binomial) { double q, qn, np, px, U; - long X, bound; + int64_t X, bound; - if (!(binomial->has_binomial) || - (binomial->nsave != n) || + if (!(binomial->has_binomial) || (binomial->nsave != n) || (binomial->psave != p)) { binomial->nsave = n; binomial->psave = p; @@ -807,8 +799,7 @@ long random_binomial_inversion(prng_t *prng_state, long n, double p, binomial_t* binomial->q = q = 1.0 - p; binomial->r = qn = exp(n * log(q)); binomial->c = np = n * p; - binomial->m = bound = - (long)min(n, np + 10.0 * sqrt(np * q + 1)); + binomial->m = bound = (int64_t)min(n, np + 10.0 * sqrt(np * q + 1)); } else { q = binomial->q; qn = binomial->r; @@ -832,7 +823,8 @@ long random_binomial_inversion(prng_t *prng_state, long n, double p, binomial_t* return X; } -long random_binomial(prng_t *prng_state, double p, long n, binomial_t * binomial) { +int64_t random_binomial(prng_t *prng_state, double p, int64_t n, + binomial_t *binomial) { double q; if (p <= 0.5) { @@ -860,7 +852,7 @@ double random_noncentral_chisquare(prng_t *prng_state, double df, double nonc) { const double n = random_gauss_zig(prng_state) + sqrt(nonc); return Chi2 + n * n; } else { - const long i = random_poisson(prng_state, nonc / 2.0); + const int64_t i = random_poisson(prng_state, nonc / 2.0); return random_chisquare(prng_state, df + 2 * i); } } @@ -938,9 +930,9 @@ double random_vonmises(prng_t *prng_state, double mu, double kappa) { } } -long random_logseries(prng_t *prng_state, double p) { +int64_t random_logseries(prng_t *prng_state, double p) { double q, r, U, V; - long result; + int64_t result; r = log(1.0 - p); @@ -952,7 +944,7 @@ long random_logseries(prng_t *prng_state, double p) { U = random_sample(prng_state); q = 1.0 - exp(r * U); if (V <= q * q) { - result = (long)floor(1 + log(V) / log(q)); + result = (int64_t)floor(1 + log(V) / log(q)); if (result < 1) { continue; } else { @@ -966,9 +958,9 @@ long random_logseries(prng_t *prng_state, double p) { } } -long random_geometric_search(prng_t *prng_state, double p) { +int64_t random_geometric_search(prng_t *prng_state, double p) { double U; - long X; + int64_t X; double sum, prod, q; X = 1; @@ -983,11 +975,11 @@ long random_geometric_search(prng_t *prng_state, double p) { return X; } -long random_geometric_inversion(prng_t *prng_state, double p) { - return (long)ceil(log(1.0 - random_sample(prng_state)) / log(1.0 - p)); +int64_t random_geometric_inversion(prng_t *prng_state, double p) { + return (int64_t)ceil(log(1.0 - random_sample(prng_state)) / log(1.0 - p)); } -long random_geometric(prng_t *prng_state, double p) { +int64_t random_geometric(prng_t *prng_state, double p) { if (p >= 0.333333333333333333333333) { return random_geometric_search(prng_state, p); } else { @@ -995,9 +987,9 @@ long random_geometric(prng_t *prng_state, double p) { } } -long random_zipf(prng_t *prng_state, double a) { +int64_t random_zipf(prng_t *prng_state, double a) { double T, U, V; - long X; + int64_t X; double am1, b; am1 = a - 1.0; @@ -1005,9 +997,9 @@ long random_zipf(prng_t *prng_state, double a) { do { U = 1.0 - random_sample(prng_state); V = random_sample(prng_state); - X = (long)floor(pow(U, -1.0 / am1)); - /* The real result may be above what can be represented in a signed - * long. It will get casted to -sys.maxint-1. Since this is + X = (int64_t)floor(pow(U, -1.0 / am1)); + /* The real result may be above what can be represented in a int64. + * It will get casted to -sys.maxint-1. Since this is * a straightforward rejection algorithm, we can just reject this value * in the rejection condition below. This function then models a Zipf * distribution truncated to sys.maxint. @@ -1036,9 +1028,9 @@ double random_triangular(prng_t *prng_state, double left, double mode, } } -long random_hypergeometric_hyp(prng_t *prng_state, long good, long bad, - long sample) { - long d1, k, z; +int64_t random_hypergeometric_hyp(prng_t *prng_state, int64_t good, int64_t bad, + int64_t sample) { + int64_t d1, k, z; double d2, u, y; d1 = bad + good - sample; @@ -1048,12 +1040,12 @@ long random_hypergeometric_hyp(prng_t *prng_state, long good, long bad, k = sample; while (y > 0.0) { u = random_sample(prng_state); - y -= (long)floor(u + y / (d1 + k)); + y -= (int64_t)floor(u + y / (d1 + k)); k--; if (k == 0) break; } - z = (long)(d2 - y); + z = (int64_t)(d2 - y); if (good > bad) z = sample - z; return z; @@ -1063,11 +1055,11 @@ long random_hypergeometric_hyp(prng_t *prng_state, long good, long bad, /* D2 = 3 - 2*sqrt(3/e) */ #define D1 1.7155277699214135 #define D2 0.8989161620588988 -long random_hypergeometric_hrua(prng_t *prng_state, long good, long bad, - long sample) { - long mingoodbad, maxgoodbad, popsize, m, d9; +int64_t random_hypergeometric_hrua(prng_t *prng_state, int64_t good, + int64_t bad, int64_t sample) { + int64_t mingoodbad, maxgoodbad, popsize, m, d9; double d4, d5, d6, d7, d8, d10, d11; - long Z; + int64_t Z; double T, W, X, Y; mingoodbad = min(good, bad); @@ -1079,7 +1071,7 @@ long random_hypergeometric_hrua(prng_t *prng_state, long good, long bad, d6 = m * d4 + 0.5; d7 = sqrt((double)(popsize - m) * sample * d4 * d5 / (popsize - 1) + 0.5); d8 = D1 * d7 + D2; - d9 = (long)floor((double)(m + 1) * (mingoodbad + 1) / (popsize + 2)); + d9 = (int64_t)floor((double)(m + 1) * (mingoodbad + 1) / (popsize + 2)); d10 = (loggam(d9 + 1) + loggam(mingoodbad - d9 + 1) + loggam(m - d9 + 1) + loggam(maxgoodbad - m + d9 + 1)); d11 = min(min(m, mingoodbad) + 1.0, floor(d6 + 16 * d7)); @@ -1094,7 +1086,7 @@ long random_hypergeometric_hrua(prng_t *prng_state, long good, long bad, if ((W < 0.0) || (W >= d11)) continue; - Z = (long)floor(W); + Z = (int64_t)floor(W); T = d10 - (loggam(Z + 1) + loggam(mingoodbad - Z + 1) + loggam(m - Z + 1) + loggam(maxgoodbad - m + Z + 1)); @@ -1123,8 +1115,8 @@ long random_hypergeometric_hrua(prng_t *prng_state, long good, long bad, #undef D1 #undef D2 -long random_hypergeometric(prng_t *prng_state, long good, long bad, - long sample) { +int64_t random_hypergeometric(prng_t *prng_state, int64_t good, int64_t bad, + int64_t sample) { if (sample > 10) { return random_hypergeometric_hrua(prng_state, good, bad, sample); } else { diff --git a/_randomgen/core_prng/src/distributions/distributions.h b/_randomgen/core_prng/src/distributions/distributions.h index 9b1a70e9e1fc..c5050d400ea6 100644 --- a/_randomgen/core_prng/src/distributions/distributions.h +++ b/_randomgen/core_prng/src/distributions/distributions.h @@ -18,6 +18,21 @@ typedef int bool; #include "numpy/npy_common.h" #include +#ifdef _WIN32 +#if _MSC_VER == 1500 + +static NPY_INLINE int64_t llabs(int64_t x) { + int64_t o; + if (x < 0) { + o = -x; + } else { + o = x; + } + return o; +} +#endif +#endif + #ifdef DLL_EXPORT #define DECLDIR __declspec(dllexport) #else @@ -36,11 +51,11 @@ typedef int bool; typedef struct s_binomial_t { int has_binomial; /* !=0: following parameters initialized for binomial */ double psave; - long nsave; + int64_t nsave; double r; double q; double fm; - long m; + int64_t m; double p1; double xm; double xl; @@ -83,8 +98,8 @@ DECLDIR double random_sample(prng_t *prng_state); DECLDIR int64_t random_positive_int64(prng_t *prng_state); DECLDIR int32_t random_positive_int32(prng_t *prng_state); -DECLDIR long random_positive_int(prng_t *prng_state); -DECLDIR unsigned long random_uint(prng_t *prng_state); +DECLDIR int64_t random_positive_int(prng_t *prng_state); +DECLDIR uint64_t random_uint(prng_t *prng_state); DECLDIR double random_standard_exponential(prng_t *prng_state); DECLDIR float random_standard_exponential_f(prng_t *prng_state); @@ -137,16 +152,18 @@ DECLDIR double random_vonmises(prng_t *prng_state, double mu, double kappa); DECLDIR double random_triangular(prng_t *prng_state, double left, double mode, double right); -DECLDIR long random_poisson(prng_t *prng_state, double lam); -DECLDIR long random_negative_binomial(prng_t *prng_state, double n, double p); -DECLDIR long random_binomial(prng_t *prng_state, double p, long n, binomial_t* binomial); -DECLDIR long random_logseries(prng_t *prng_state, double p); -DECLDIR long random_geometric_search(prng_t *prng_state, double p); -DECLDIR long random_geometric_inversion(prng_t *prng_state, double p); -DECLDIR long random_geometric(prng_t *prng_state, double p); -DECLDIR long random_zipf(prng_t *prng_state, double a); -DECLDIR long random_hypergeometric(prng_t *prng_state, long good, long bad, - long sample); +DECLDIR int64_t random_poisson(prng_t *prng_state, double lam); +DECLDIR int64_t random_negative_binomial(prng_t *prng_state, double n, + double p); +DECLDIR int64_t random_binomial(prng_t *prng_state, double p, int64_t n, + binomial_t *binomial); +DECLDIR int64_t random_logseries(prng_t *prng_state, double p); +DECLDIR int64_t random_geometric_search(prng_t *prng_state, double p); +DECLDIR int64_t random_geometric_inversion(prng_t *prng_state, double p); +DECLDIR int64_t random_geometric(prng_t *prng_state, double p); +DECLDIR int64_t random_zipf(prng_t *prng_state, double a); +DECLDIR int64_t random_hypergeometric(prng_t *prng_state, int64_t good, + int64_t bad, int64_t sample); DECLDIR uint64_t random_interval(prng_t *prng_state, uint64_t max); DECLDIR uint64_t random_bounded_uint64(prng_t *prng_state, uint64_t off, diff --git a/_randomgen/core_prng/tests/test_numpy_mt19937.py b/_randomgen/core_prng/tests/test_numpy_mt19937.py index 9ff45504fdb2..dbe25172843a 100644 --- a/_randomgen/core_prng/tests/test_numpy_mt19937.py +++ b/_randomgen/core_prng/tests/test_numpy_mt19937.py @@ -298,16 +298,14 @@ def test_repeatability(self): res = hashlib.md5(val.view(np.int8)).hexdigest() print(tgt[np.dtype(dt).name] == res) - # TODO - # assert_(tgt[np.dtype(dt).name] == res) + assert_(tgt[np.dtype(dt).name] == res) # bools do not depend on endianess mt19937.seed(1234) val = self.rfunc(0, 2, size=1000, dtype=bool).view(np.int8) res = hashlib.md5(val).hexdigest() print(tgt[np.dtype(bool).name] == res) - # TODO - # assert_(tgt[np.dtype(bool).name] == res) + assert_(tgt[np.dtype(bool).name] == res) def test_repeatability_broadcasting(self): @@ -1082,9 +1080,6 @@ def setup(self): def set_seed(self): random.seed(self.seed) - # TODO: Include test for randint once it can broadcast - # Can steal the test written in PR #6938 - def test_uniform(self): low = [0] high = [1] diff --git a/_randomgen/demo.py b/_randomgen/demo.py deleted file mode 100644 index 30b0984221d2..000000000000 --- a/_randomgen/demo.py +++ /dev/null @@ -1,103 +0,0 @@ -import timeit - -from core_prng import Xoroshiro128, ThreeFry, MT19937, \ - Xorshift1024, PCG64, Philox, DSFMT -from core_prng.generator import RandomGenerator - -print(RandomGenerator().random_integer(32)) -print(RandomGenerator(Xoroshiro128()).random_integer()) -print(RandomGenerator(ThreeFry()).random_integer()) - -print('\n' * 3) -print('Check random_sample') -rg = RandomGenerator() -print(rg.state) -print(rg.random_sample()) -print(rg.state) -print(rg.random_sample()) -print(rg.random_sample((3))) -print(rg.random_sample((3, 1))) -print(rg.state) -import numpy as np - -a = rg.random_sample((1, 1), dtype=np.float32) -print(a) -print(a.dtype) -print(rg.state) - -print('\n' * 3) -print('Check set/get state') -state = rg.state -print(rg.state) -print(rg.random_integer()) -print(rg.state) -rg.state = state -print(rg.random_integer()) - -print(RandomGenerator(Xoroshiro128()).state) -rg = RandomGenerator(ThreeFry()) -print(rg.state) -rg.random_integer() -print(rg.state) -rg = RandomGenerator(MT19937()) -state = rg.state -print(state) -rg.state = state -print(rg.random_integer()) -print(rg.random_integer(32)) -print(rg.random_sample()) - -rg = RandomGenerator(Xorshift1024()) -state = rg.state -print(state) -rg.state = state - -rg = RandomGenerator(PCG64()) -state = rg.state -print(state) -rg.state = state - -rg = RandomGenerator(Philox()) -state = rg.state -print(state) -rg.state = state - -rg = RandomGenerator(DSFMT()) -state = rg.state -print(state) -rg.state = state - - -PRNGS = [MT19937, PCG64, Philox, ThreeFry, Xoroshiro128, Xorshift1024, DSFMT] - -setup = """ -from core_prng import {module} -m = {module}() -m._benchmark(701) -""" -import pandas as pd -res = [] -for p in PRNGS: - module = p.__name__ - print(module) - t = timeit.timeit("m._benchmark(10000000)", setup.format(module=module), - number=10) - res.append(pd.Series({'module': module, 'ms': 1000 * - t / 10, 'rps': int(10000000 / (t/10))})) - #print('{:0.2f} ms'.format()) - # print('{:,} randoms per second'.format())) -res = pd.DataFrame(res) -print(res.set_index('module').sort_values('ms')) - -res = [] -for p in PRNGS: - module = p.__name__ - print(module) - t = timeit.timeit("m._benchmark(10000000, 'double')", setup.format(module=module), - number=10) - res.append(pd.Series({'module': module, 'ms': 1000 * - t / 10, 'rps': int(10000000 / (t/10))})) - #print('{:0.2f} ms'.format()) - # print('{:,} randoms per second'.format())) -res = pd.DataFrame(res) -print(res.set_index('module').sort_values('ms'))