4

I am trying to cythonise something I did which involves random number generation inside a parallelised loop. I wanted to use mtrand but since it's Python code it can't work from a nogil block and for some reason mtrand's .pyx isn't exposed for the rest of us to use.

I know I can use rand or any other C RNG (e.g. gsl); is there a more standard way?

marco
  • 806
  • 1
  • 7
  • 17

1 Answers1

3

You have summed up the situation correctly. As of this writing, you can do one of three things:

  • Modify NumPy to allow sharing the declarations in mtrand.pxd

  • Use NumPy's random generators through their default interface (perhaps you could store all the random numbers in advance outside of the nogil block?)

  • Use a random number generator written in C (or possibly C++ if you are having Cython generate C++ code).

Honestly, I'd probably do the last one. If you can use C++ 11, there are several good random number generators now included in the C++ standard library that you could use.

IanH
  • 10,250
  • 1
  • 28
  • 32
  • In practical terms #3 is the most realistic one. However I am puzzled as to why `mtrand` is not exposed. If I am using the full `scipy` stack, why should I use a different RNG? – marco Jan 08 '15 at 14:52
  • That's a good question. There are a variety of Cython files included throughout the scipy stack that aren't exposed as public interfaces. The only reason I can see for it is that many of the interfaces weren't really meant to be publicly supported parts of numpy/scipy. On the other hand, I'd bet that more and more of these interfaces will be made public in the future. I just stumbled on some relevant discussion at the Cython users discussion board: https://groups.google.com/forum/#!topic/cython-users/9UGMi_b3tVo – IanH Jan 08 '15 at 17:56