简体   繁体   中英

How to specify a random number generator in numpy

I'm building a monte carlo simulation using python, and have thus far been using numpy to generate my random variates. However, I've just learned that numpy uses the Mersenne Twister algorithm to produce its random numbers, which based on my limited understanding is not desireable in monte carlo simulations. I'd much prefer to use MRG32k3a, but I'd also like to take advantage of numpy's distribution functions. Is there any way to make numpy use a generator of my choice, or is there another library that will give me the same functionality with the option of using my preferred generator?

Why don't you code it up yourself in python?

I found an example implementation of the algorithm at http://simul.iro.umontreal.ca/rng/MRG32k3a.c (with many others at http://www-labs.iro.umontreal.ca/~simul/rng )

If you are worried about speed (but profile it first!) you could wrap the C or F code and call it directly from python using eg CFFI, f2py etc - see eg

https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

But given it's only a few lines of C coding it in python shouldn't be too challenging, right?

You could also submit a pull request at https://github.com/bashtage/randomgen (or ask the author nicely to include your chosen generator).

I'm sorry this doesn't address your need for distributions, but perhaps it's a start and a better answer will come along.

Also perhaps work out why Mersenne is no good for your own purposes - have you tested it and what figures of merit are you using to decide?

https://scicomp.stackexchange.com/questions/23547/parallel-mersenne-twister-for-monte-carlo might be a useful reference for you.

Can you do any precomputation?

Hope that helps in some small way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM