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

后端 未结 1 1300
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 14:39

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 f

相关标签:
1条回答
  • 2020-12-18 14:56

    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.)

    0 讨论(0)
提交回复
热议问题