Python 中 numpy.random 和 random.random 的区别 [英] Differences between numpy.random and random.random in Python

查看:35
本文介绍了Python 中 numpy.random 和 random.random 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 Python 脚本.我在其他人的代码中启发了自己,所以我最终将 numpy.random 模块用于某些事情(例如,用于创建从二项分布中获取的随机数数组),而在其他地方我使用模块 random.random.

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.

有人可以告诉我两者之间的主要区别吗?查看两者的文档网页,在我看来 numpy.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?

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

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.

推荐答案

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

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.

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

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.random.seed() is thread-safe (or at least, I haven't found any evidence to the contrary).

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

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.

否则,他们都使用梅森扭曲序列来生成他们的随机数,并且他们'两者都是完全确定的——也就是说,如果您知道一些关键信息,就可以绝对确定地进行预测 接下来是什么号码.因此,numpy.random 和 random.random 都不适合任何 严重的加密用途.但是因为序列非常非常长,所以在您不担心人们试图对您的数据进行逆向工程的情况下,两者都可以生成随机数.这也是需要为随机值做种子的原因——如果你每次都从同一个地方开始,你总是会得到相同的随机数序列!

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!

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

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.

这篇关于Python 中 numpy.random 和 random.random 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆