Generating random numbers with a stream using cython
I am trying to do cythonise something I have been doing that involves generating random numbers inside a parallelized loop. I wanted to use mtrand
, but since this is Python code, it cannot work from nogil
within a block and for some reason mtrand
.pyx is not visible for the rest of us to use.
I know I can use rand
or any other C RNG (for example gsl
); is there a more standard way?
source to share
You have formulated the situation correctly. At the time of this writing, you can do one of three things:
-
Change NumPy to allow ad sharing in mtrand.pxd
-
Use NumPy random generators through your default interface (perhaps you could have stored all random numbers outside the block beforehand
nogil
?) -
Use a random number generator written in C (or perhaps C ++ if you have Cython generating C ++ code).
Honestly, I will probably do the latter. If you can use C ++ 11, there are some good random number generators now included in the C ++ standard library that you could use.
source to share