Skip to content

Commit

Permalink
Merge 3fe178d into 055b2e1
Browse files Browse the repository at this point in the history
  • Loading branch information
bashtage committed Mar 31, 2019
2 parents 055b2e1 + 3fe178d commit 60c3f39
Show file tree
Hide file tree
Showing 10 changed files with 6,131 additions and 35 deletions.
36 changes: 34 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
RandomGen
=========

|Travis Build Status| |Appveyor Build Status| |PyPI version|

Random Number Generator using settable Basic RNG interface for future
NumPy RandomState evolution.

Continuous Integration
~~~~~~~~~~~~~~~~~~~~~~

|Travis Build Status| |Appveyor Build Status|

Coverage
~~~~~~~~

|Coverage Status| |codecov|

Latest Release
~~~~~~~~~~~~~~

|PyPI version| |Anacnoda Cloud|

License
~~~~~~~

|License| |License| |DOI|

This is a library and generic interface for alternative random
generators in Python and NumPy.

Expand Down Expand Up @@ -257,6 +275,8 @@ The separate generators are importable from ``randomgen``
rg = RandomGenerator(MT19937())
rg.random_sample(100)
.. _license-1:

License
-------

Expand Down Expand Up @@ -316,5 +336,17 @@ NumPy’s mt19937.
:target: https://travis-ci.org/bashtage/randomgen
.. |Appveyor Build Status| image:: https://ci.appveyor.com/api/projects/status/odc5c4ukhru5xicl/branch/master?svg=true
:target: https://ci.appveyor.com/project/bashtage/randomgen/branch/master
.. |Coverage Status| image:: https://coveralls.io/repos/github/bashtage/randomgen/badge.svg?branch=coverage
:target: https://coveralls.io/github/bashtage/randomgen?branch=coverage
.. |codecov| image:: https://codecov.io/gh/bashtage/randomgen/branch/master/graph/badge.svg
:target: https://codecov.io/gh/bashtage/randomgen
.. |PyPI version| image:: https://badge.fury.io/py/randomgen.svg
:target: https://pypi.org/project/randomgen/
.. |Anacnoda Cloud| image:: https://anaconda.org/bashtage/randomgen/badges/version.svg
:target: https://anaconda.org/bashtage/randomgen
.. |License| image:: https://img.shields.io/badge/License-NCSA-blue.svg
:target: https://opensource.org/licenses/NCSA
.. |License| image:: https://img.shields.io/badge/License-BSD%203--Clause-blue.svg
:target: https://opensource.org/licenses/BSD-3-Clause
.. |DOI| image:: https://zenodo.org/badge/122181085.svg
:target: https://zenodo.org/badge/latestdoi/122181085
11 changes: 0 additions & 11 deletions randomgen/bounded_integers.pxd.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ ctypedef np.npy_bool bool_t

from randomgen.common cimport brng_t

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)
}

cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
"""Mask generator for use in bounded random numbers"""
# Smallest bit mask >= max
Expand Down
11 changes: 11 additions & 0 deletions randomgen/bounded_integers.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ from randomgen.distributions cimport *

np.import_array()

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)
}

{{
py:
type_info = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000ULL'),
Expand Down
11 changes: 1 addition & 10 deletions randomgen/generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ except ImportError:
from dummy_threading import Lock

from randomgen.bounded_integers cimport *
from randomgen.bounded_integers import _randint_types
from randomgen.common cimport *
from randomgen.distributions cimport *
from randomgen.xoroshiro128 import Xoroshiro128
import randomgen.pickle

np.import_array()

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)
}

cdef class RandomGenerator:
"""
Expand Down
12 changes: 1 addition & 11 deletions randomgen/legacy/_legacy.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ except ImportError:
from dummy_threading import Lock

from randomgen.bounded_integers cimport *
from randomgen.bounded_integers import _randint_types
from randomgen.common cimport cont, disc, double_fill, CONS_NONE, \
CONS_POSITIVE, CONS_NON_NEGATIVE, CONS_BOUNDED_0_1
from randomgen.distributions cimport brng_t, random_double_fill
Expand All @@ -26,17 +27,6 @@ import randomgen.pickle

np.import_array()

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)
}


cdef class _LegacyGenerator:
"""
Expand Down
14 changes: 13 additions & 1 deletion randomgen/mt19937.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import operator
from libc.stdlib cimport malloc, free
from cpython.pycapsule cimport PyCapsule_New

try:
from threading import Lock
except ImportError:
from dummy_threading import Lock

import numpy as np
cimport numpy as np

Expand Down Expand Up @@ -122,11 +127,13 @@ cdef class MT19937:
cdef object _ctypes
cdef object _cffi
cdef object _generator
cdef public object lock

def __init__(self, seed=None):
self.rng_state = <mt19937_state *>malloc(sizeof(mt19937_state))
self._brng = <brng_t *>malloc(sizeof(brng_t))
self.seed(seed)
self.lock = Lock()

self._brng.state = <void *>self.rng_state
self._brng.next_uint64 = &mt19937_uint64
Expand Down Expand Up @@ -206,7 +213,12 @@ cdef class MT19937:
raise ValueError("Seed must be between 0 and 2**32 - 1")
mt19937_seed(self.rng_state, seed)
except TypeError:
obj = np.asarray(seed).astype(np.int64, casting='safe')
obj = np.asarray(seed)
if obj.size == 0:
raise ValueError("Seed must be non-empty")
obj = obj.astype(np.int64, casting='safe')
if obj.ndim != 1:
raise ValueError("Seed array must be 1-d")
if ((obj > int(2**32 - 1)) | (obj < 0)).any():
raise ValueError("Seed must be between 0 and 2**32 - 1")
obj = obj.astype(np.uint32, casting='unsafe', order='C')
Expand Down

0 comments on commit 60c3f39

Please sign in to comment.