Commit 90cd6add authored by Till Hoffmann's avatar Till Hoffmann Committed by GitHub

Add mt19937 random number generator to libcpp. (GH-4746)

parent 16987687
from libc.stdint cimport uint_fast32_t
cdef extern from "<random>" namespace "std" nogil:
cdef cppclass mt19937:
ctypedef uint_fast32_t result_type
mt19937() except +
mt19937(result_type seed) except +
result_type operator()() except +
result_type min() except +
result_type max() except +
void discard(size_t z) except +
void seed(result_type seed) except +
# mode: run
# tag: cpp, cpp11
from libcpp.random cimport mt19937
def mt19937_seed_test():
"""
>>> print(mt19937_seed_test())
1608637542
"""
cdef mt19937 rd = mt19937(42)
return rd()
def mt19937_reseed_test():
"""
>>> print(mt19937_reseed_test())
1608637542
"""
cdef mt19937 rd
rd.seed(42)
return rd()
def mt19937_min_max():
"""
>>> x, y = mt19937_min_max()
>>> print(x)
0
>>> print(y) # 2 ** 32 - 1 because mt19937 is 32 bit.
4294967295
"""
cdef mt19937 rd
return rd.min(), rd.max()
def mt19937_discard(z):
"""
>>> x, y = mt19937_discard(13)
>>> print(x)
1972458954
>>> print(y)
1972458954
"""
cdef mt19937 rd = mt19937(42)
# Throw away z random numbers.
rd.discard(z)
a = rd()
# Iterate over z random numbers.
rd.seed(42)
for _ in range(z + 1):
b = rd()
return a, b
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment