python random 和numpy 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.

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: numpy.random.randn是一个用于生成随机数的函数,返回的是一个由浮点数组成的指定形状的数组,这些浮点数是从标准正态分布随机抽样得到的。 具体来说,numpy.random.randn函数的语法如下: ```python numpy.random.randn(d0, d1, ..., dn) ``` 其,d0, d1, ..., dn 是表示生成数组形状的参数,可以是整数或者整数元组。函数会返回一个形状为 (d0, d1, ..., dn) 的数组,其的元素是从标准正态分布抽样得到的浮点数。 例如,下面的代码生成了一个形状为 (2, 3) 的数组,其的元素是从标准正态分布抽样得到的: ```python import numpy as np arr = np.random.randn(2, 3) print(arr) ``` 输出: ``` [[-1.33307884 0.8263848 0.05024193] [-1.04358602 0.497614 1.09213628]] ``` ### 回答2: numpy.random.randn是numpy的一个函数,用于生成服从标准正态分布(平均值为0,标准差为1)的随机数。它可以根据指定的大小来生成一个指定形状的数组。下面是对numpy.random.randn函数的详细解释。 首先,numpyPython的一个重要的科学计算库,提供了很多用于数组操作和数学计算的功能。而numpy.random模块是numpy的一个子模块,提供了伪随机数生成器的功能。其,randn函数用于生成服从标准正态分布的随机数序列。 语法格式:numpy.random.randn(d0, d1, ..., dn),其d0, d1, ..., dn表示生成的随机数序列的维度,可以是整数或整数序列。 参数说明:d0, d1, ..., dn用于指定生成随机数序列的维度,如果没有指定,则默认为一个随机数。 返回值:返回一个形状为(d0, d1, ..., dn)的数组,数组的元素服从标准正态分布。 例如,生成一个形状为(2, 3)的二维数组,其的元素服从标准正态分布,可以使用以下代码: import numpy as np arr = np.random.randn(2, 3) print(arr) 运行结果可能为: [[-0.24616705 0.25069005 1.41882856] [ 1.06692249 0.20792445 0.10625843]] 在生成的数组,每个元素都是一个服从标准正态分布的随机数。 总之,numpy.random.randn函数是用于生成服从标准正态分布的随机数序列的。它可以根据指定的大小来生成一个指定形状的数组,提供了强大的随机数生成功能,在数据科学和工程计算常被使用。 ### 回答3: numpy.random.randn是numpy用于生成符合标准正态分布(均值为0,标准差为1)的随机数的函数。该函数的参数是一个或多个整数,用于指定返回随机数的维度,返回值是一个具有指定维度的numpy数组。 使用方法如下: 1. 首先,需要导入numpy库:import numpy as np 2. 然后,可以使用np.random.randn来生成随机数。 下面是一个简单的例子,生成一个3x3的随机数组: import numpy as np rand_arr = np.random.randn(3, 3) print(rand_arr) 执行以上代码,可能得到的输出结果类似于: [[-0.28790072 -1.30127789 0.28642234] [ 0.30540302 -0.41940331 0.02838139] [-1.03656423 0.12807012 -0.20615022]] 可以看到,生成的随机数符合标准正态分布。如果需要生成其他均值和方差的正态分布随机数,可以使用numpy.random.normal函数。 总之,numpy.random.randn函数是一个方便生成符合标准正态分布随机数的函数,可以根据需要指定维度生成相应的随机数数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值