Skip to content

Commit

Permalink
fix(nogil): make functions callable with nogil
Browse files Browse the repository at this point in the history
  • Loading branch information
adrizein committed Apr 23, 2017
1 parent 69643e9 commit 594cc6e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions cyrandom/__init__.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from cyrandom cimport *
16 changes: 8 additions & 8 deletions cyrandom/cyrandom.pxd
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cpdef double random()
cpdef double random() nogil

cdef unsigned short bit_length(unsigned long n)
cdef unsigned short bit_length(unsigned long n) nogil

cdef unsigned long getrandbits(unsigned short k)
cdef unsigned long getrandbits(unsigned short k) nogil

cpdef long randrange(long start, long stop)
cpdef long randrange(long start, long stop) nogil

cpdef long randint(long a, long b)
cpdef long randint(long a, long b) nogil

cpdef double uniform(double a, double b)
cpdef double uniform(double a, double b) nogil

cpdef double triangular(double low=?, double high=?, double mode=?)
cpdef double triangular(double low=?, double high=?, double mode=?) nogil

cpdef long triangular_int(long low, long high, long mode)
cpdef long triangular_int(long low, long high, long mode) nogil
18 changes: 9 additions & 9 deletions cyrandom/cyrandom.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ from ._mersenne cimport genrand_int32, genrand_res53
random_seed()


cpdef double random():
cpdef double random() nogil:
"""Return a double between 0.0 and 1.0
"""
return genrand_res53()


cdef unsigned short bit_length(unsigned long n):
cdef unsigned short bit_length(unsigned long n) nogil:
cdef unsigned short length = 0
while n != 0:
length += 1
n >>= 1
return length


cdef unsigned long getrandbits(unsigned short k):
cdef unsigned long getrandbits(unsigned short k) nogil:
return genrand_int32() >> (32 - k)


cpdef long randrange(long start, long stop):
cpdef long randrange(long start, long stop) nogil:
"""Choose a random item from range(start, stop).
This fixes the problem with randint() which includes the
Expand All @@ -41,13 +41,13 @@ cpdef long randrange(long start, long stop):
return start + _randbelow(width)


cpdef long randint(long a, long b):
cpdef long randint(long a, long b) nogil:
"""Return random integer in range [a, b], including both end points.
"""
return randrange(a, b+1)


cdef unsigned long _randbelow(unsigned long n):
cdef unsigned long _randbelow(unsigned long n) nogil:
"""Return a random int in the range [0,n). Raises ValueError if n==0.
"""
_getrandbits = getrandbits
Expand Down Expand Up @@ -104,13 +104,13 @@ def choose_weighted(tuple population, tuple cum_weights):
return population[bisect(cum_weights, random() * total)]


cpdef double uniform(double a, double b):
cpdef double uniform(double a, double b) nogil:
"""Get a random number in the range [a, b) or [a, b] depending on rounding.
"""
return a + (b-a) * genrand_res53()


cpdef double triangular(double low=0.0, double high=1.0, double mode=0.5):
cpdef double triangular(double low=0.0, double high=1.0, double mode=0.5) nogil:
"""Triangular distribution.
Continuous distribution bounded by given lower and upper limits,
Expand All @@ -127,7 +127,7 @@ cpdef double triangular(double low=0.0, double high=1.0, double mode=0.5):
return low + (high - low) * sqrt(u * c)


cpdef long triangular_int(long low, long high, long mode):
cpdef long triangular_int(long low, long high, long mode) nogil:
cdef double c, u = genrand_res53()
c = (mode - low) / (high - low)
if u > c:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
],
keywords='cyrandom random rng cython',
packages=['cyrandom'],
package_data={'cyrandom': ['cyrandom.pxd']},
package_data={'cyrandom': ['cyrandom.pxd', '__init__.pxd']},
ext_modules=ext)

0 comments on commit 594cc6e

Please sign in to comment.