Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
90cd6add
Commit
90cd6add
authored
Apr 20, 2022
by
Till Hoffmann
Committed by
GitHub
Apr 20, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add mt19937 random number generator to libcpp. (GH-4746)
parent
16987687
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
0 deletions
+69
-0
Cython/Includes/libcpp/random.pxd
Cython/Includes/libcpp/random.pxd
+14
-0
tests/run/cpp_stl_random.pyx
tests/run/cpp_stl_random.pyx
+55
-0
No files found.
Cython/Includes/libcpp/random.pxd
0 → 100644
View file @
90cd6add
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
+
tests/run/cpp_stl_random.pyx
0 → 100644
View file @
90cd6add
# 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment