What is the difference between numpy.random's Generator class and np.random methods?

一个人想着一个人 提交于 2020-12-23 02:48:12

问题


I have been using numpy's random functionality for a while, by calling methods such as np.random.choice() or np.random.randint() etc. I just now found about the ability to create a default_rng object, or other Generator objects:

from numpy.random import default_rng
gen = default_rng()
random_number = gen.integers(10)

So far I would have always used

np.random.randint(10)

instead, and I am wondering what the difference between both ways is.

The only benefit I can think of would be keeping track of multiple seeds, or wanting to use specific PRNGs, but maybe there are also differences for a more generic use-case?


回答1:


numpy.random.* functions (including numpy.random.binomial) make use of a global random generator object which is shared across the application. On the other hand, default_rng() is a self-contained Generator object that doesn't rely on global state.

If you don't care about reproducible "randomness" in your application, these two approaches are equivalent for the time being. Although NumPy's new RNG policy discourages the use of global state in general, it did not deprecate any numpy.random.* functions in version 1.17, although a future version of NumPy might.

Note also that because numpy.random.* functions rely on a global random generator object that isn't thread-safe, these functions can cause race conditions if your application uses multiple threads. (Generator objects are not thread-safe, either, but there are ways to generate random numbers via multithreading, without the need to share random generator objects across threads.)



来源:https://stackoverflow.com/questions/60599149/what-is-the-difference-between-numpy-randoms-generator-class-and-np-random-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!