np.random.random()和 np.random.random_sample()是一致的

2022-07-29,

从官方文档上来看。我并没有发现np.random.random()np.random.random_sample()在用法上有什么不同之处,所以它们两个在本质上都是一样的,随机给出设定的size尺寸的位于[0,1)半开半闭区间上的随机数。
代码如下:

# -*- coding: utf-8 -*- """
np.random.random()
""" import numpy as np class Debug: def __init__(self): self.sample = None self.sample1 = [] def mainProgram(self): np.random.seed(2) self.sample = np.random.random_sample(size=(2, 2)) self.sample1 = np.random.random((2, 2)) print("The value of sample is: ") print(self.sample) print("The value of sample1 is: ") print(self.sample1) if __name__ == '__main__': main = Debug() main.mainProgram() """
The value of sample is: 
[[0.4359949  0.02592623]
 [0.54966248 0.43532239]]
The value of sample1 is: 
[[0.4203678  0.33033482]
 [0.20464863 0.61927097]]
""" 

此处均给出了尺寸为(2,2)的二维随机数数组。

《np.random.random()和 np.random.random_sample()是一致的.doc》

下载本文的Word格式文档,以方便收藏与打印。