Python 中 numpy.random 和 random.random 的区别

发布于 2024-11-29 03:45:45 字数 383 浏览 4 评论 0 原文

我有一个很大的Python脚本。我在其他人的代码中受到启发,因此我最终使用 numpy.random 模块来完成某些事情(例如,创建从二项式分布中获取的随机数数组),而在其他地方我使用模块random.random

有人可以告诉我两者之间的主要区别吗? 查看两者的文档网页,在我看来 numpy.random 只是有更多方法,但我不清楚随机数的生成有何不同。

我之所以问这个问题是因为我需要为我的主程序播种以进行调试。但除非我在导入的所有模块中使用相同的随机数生成器,否则它不起作用,这是正确的吗?

另外,我在另一篇文章中读到了关于不使用 numpy.random.seed() 的讨论,但我真的不明白为什么这是一个坏主意。如果有人向我解释为什么会出现这种情况,我将非常感激。

I have a big script in Python. I inspired myself in other people's code so I ended up using the numpy.random module for some things (for example for creating an array of random numbers taken from a binomial distribution) and in other places I use the module random.random.

Can someone please tell me the major differences between the two?
Looking at the doc webpage for each of the two it seems to me that numpy.random just has more methods, but I am unclear about how the generation of the random numbers is different.

The reason why I am asking is because I need to seed my main program for debugging purposes. But it doesn't work unless I use the same random number generator in all the modules that I am importing, is this correct?

Also, I read here, in another post, a discussion about NOT using numpy.random.seed(), but I didn't really understand why this was such a bad idea. I would really appreciate if someone explain me why this is the case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空‖城人不在 2024-12-06 03:45:45

您已经做出了许多正确的观察!

除非您想为两个随机生成器播种,否则从长远来看,选择一个生成器或另一个生成器可能更简单。但如果您确实需要同时使用两者,那么是的,您还需要为它们提供种子,因为它们彼此独立地生成随机数。

对于 numpy.random.seed() ,主要困难是它不是线程安全的 - 也就是说,如果您有 许多不同的执行线程,因为如果两个不同的线程正在执行该函数,则不能保证它可以工作 同时。如果您不使用线程,并且您可以合理地预期将来不需要以这种方式重写程序,那么 numpy.random.seed() 应该没问题。如果有任何理由怀疑您将来可能需要线程,从长远来看,按照建议进行操作会更安全,并且 创建 numpy.random.Random 类的本地实例。据我所知,random.seed() 是线程安全的(或者至少,我没有找到任何相反的证据)。

numpy.random 库包含一些科学研究中常用的额外概率分布,以及一些用于生成随机数据数组的便捷函数。 random.random 库更加轻量级,如果您不进行科学研究或其他类型的统计工作,那么应该没问题。

否则,它们都使用 梅森扭曲序列 来生成随机数,并且它们都是完全确定性 - 也就是说,如果您知道一些关键信息,则可以绝对确定地进行预测接下来会出现什么数字。因此,numpy.random 和 random.random 都不适合任何 严重的加密用途。但由于序列非常非常长,因此在您不担心人们试图对您的数据进行逆向工程的情况下,两者都可以很好地生成随机数。这也是需要播种随机值的原因 - 如果每次从同一个位置开始,您将始终获得相同的随机数序列!

附带说明一下,如果您确实需要加密级别的随机性,则应该使用 secrets 模块,或类似 Crypto.Random(如果您使用的是早于 Python 3.6 的 Python 版本)。

You have made many correct observations already!

Unless you'd like to seed both of the random generators, it's probably simpler in the long run to choose one generator or the other. But if you do need to use both, then yes, you'll also need to seed them both, because they generate random numbers independently of each other.

For numpy.random.seed(), the main difficulty is that it is not thread-safe - that is, it's not safe to use if you have many different threads of execution, because it's not guaranteed to work if two different threads are executing the function at the same time. If you're not using threads, and if you can reasonably expect that you won't need to rewrite your program this way in the future, numpy.random.seed() should be fine. If there's any reason to suspect that you may need threads in the future, it's much safer in the long run to do as suggested, and to make a local instance of the numpy.random.Random class. As far as I can tell, random.seed() is thread-safe (or at least, I haven't found any evidence to the contrary).

The numpy.random library contains a few extra probability distributions commonly used in scientific research, as well as a couple of convenience functions for generating arrays of random data. The random.random library is a little more lightweight, and should be fine if you're not doing scientific research or other kinds of work in statistics.

Otherwise, they both use the Mersenne twister sequence to generate their random numbers, and they're both completely deterministic - that is, if you know a few key bits of information, it's possible to predict with absolute certainty what number will come next. For this reason, neither numpy.random nor random.random is suitable for any serious cryptographic uses. But because the sequence is so very very long, both are fine for generating random numbers in cases where you aren't worried about people trying to reverse-engineer your data. This is also the reason for the necessity to seed the random value - if you start in the same place each time, you'll always get the same sequence of random numbers!

As a side note, if you do need cryptographic level randomness, you should use the secrets module, or something like Crypto.Random if you're using a Python version earlier than Python 3.6.

星星的軌跡 2024-12-06 03:45:45

来自Python for Data Analysis,模块numpy.random补充Python random 具有从多种概率分布中高效生成样本值整个数组的函数。

相比之下,Python 的内置 random 模块一次仅采样一个值,而 numpy.random 可以更快地生成非常大的样本。使用 IPython 魔术函数 %timeit 我们可以看到哪个模块执行得更快:

In [1]: from random import normalvariate
In [2]: N = 1000000

In [3]: %timeit samples = [normalvariate(0, 1) for _ in xrange(N)]
1 loop, best of 3: 963 ms per loop

In [4]: %timeit np.random.normal(size=N)
10 loops, best of 3: 38.5 ms per loop

From Python for Data Analysis, the module numpy.random supplements the Python random with functions for efficiently generating whole arrays of sample values from many kinds of probability distributions.

By contrast, Python's built-in random module only samples one value at a time, while numpy.random can generate very large sample faster. Using IPython magic function %timeit one can see which module performs faster:

In [1]: from random import normalvariate
In [2]: N = 1000000

In [3]: %timeit samples = [normalvariate(0, 1) for _ in xrange(N)]
1 loop, best of 3: 963 ms per loop

In [4]: %timeit np.random.normal(size=N)
10 loops, best of 3: 38.5 ms per loop
妳是的陽光 2024-12-06 03:45:45

令我惊讶的是,randint(a, b) 方法同时存在于 numpy.randomrandom 中,但它们对于上限有不同的行为。

random.randint(a, b) 返回一个随机整数 N,使得 a <= N <= b。 randrange(a, b+1) 的别名。它包含b随机文档

但是,如果您调用 numpy.random.randint(a , b),它将返回低值(包含)到高值(不包含)。 Numpy 文档

It surprised me the randint(a, b) method exists in both numpy.random and random, but they have different behaviors for the upper bound.

random.randint(a, b) returns a random integer N such that a <= N <= b. Alias for randrange(a, b+1). It has b inclusive. random documentation

However if you call numpy.random.randint(a, b), it will return low(inclusive) to high(exclusive). Numpy documentation

一念一轮回 2024-12-06 03:45:45

种子的来源和使用的分布配置文件将影响输出 - 如果您正在寻找加密随机性,从 os.urandom() 播种将从设备聊天(即以太网或磁盘)中获得几乎真实的随机字节(即/ BSD 上的 dev/random)

这将避免您提供种子并因此生成确定性随机数。然而,随机调用允许你将数字拟合到一个分布(我称之为科学随机性 - 最终你想要的只是随机数的钟形曲线分布,numpy 最擅长解决这个问题。

所以是的,坚持使用一个生成器,但决定你想要什么随机性——随机,但绝对来自分布曲线,或者在没有量子设备的情况下尽可能随机。

The source of the seed and the distribution profile used are going to affect the outputs - if you are looking for cryptgraphic randomness, seeding from os.urandom() will get nearly real random bytes from device chatter (ie ethernet or disk) (ie /dev/random on BSD)

this will avoid you giving a seed and so generating determinisitic random numbers. However the random calls then allow you to fit the numbers to a distribution (what I call scientific random ness - eventually all you want is a bell curve distribution of random numbers, numpy is best at delviering this.

SO yes, stick with one generator, but decide what random you want - random, but defitniely from a distrubtuion curve, or as random as you can get without a quantum device.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文