各种numpy随机函数之间的差异

时间:2013-09-16 13:25:20

标签: python numpy

numpy.random模块定义了以下4个函数,这些函数似乎都从连续的均匀分布返回[0,1.0]之间的浮点数。这些功能之间有什么区别(如果有的话)?

  

random_sample([size])在半开区间[0.0,1.0]中返回随机浮点数。

     

random([size])在半开区间[0.0,1.0]中返回随机浮点数。

     

ranf([size])在半开区间[0.0,1.0]中返回随机浮点数。

     

sample([size])在半开区间[0.0,1.0]中返回随机浮点数。

---------------------------编辑跟随------------------ ---------------------

我在支持@ askewchan的numpy.random源代码中找到了以下内容:

# Some aliases:
ranf = random = sample = random_sample
__all__.extend(['ranf','random','sample'])

2 个答案:

答案 0 :(得分:35)

没有

他们只是random_sample的别名:

In [660]: np.random.random
Out[660]: <function random_sample>

In [661]: np.random.ranf
Out[661]: <function random_sample>

In [662]: np.random.sample
Out[662]: <function random_sample>

In [663]: np.random.random_sample is np.random.random
Out[663]: True

In [664]: np.random.random_sample is np.random.ranf
Out[664]: True

In [665]: np.random.random_sample is np.random.sample
Out[665]: True

答案 1 :(得分:0)

我得到了不同的答案。

print(np.random.random)
print(np.random.ranf)
print(np.random.sample)
print(np.random.rand)
print(np.random.random_sample is np.random.random)
print(np.random.random_sample is np.random.ranf)
print(np.random.random_sample is np.random.sample)


<built-in method random of numpy.random.mtrand.RandomState object at 0x000001FC2C459D08>
<built-in function ranf>
<built-in function sample>
<built-in method rand of numpy.random.mtrand.RandomState object at 0x000001FC2C459D08>
False
False
False
相关问题