# numpy的random模块详解

#### 1、Simple random data

rand(d0, d1, …, dn) Random values in a given shape.
randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” distribution.
randint(low[, high, size, dtype]) Return random integers from low (inclusive) to high (exclusive).
random_integers(low[, high, size]) Random integers of type np.int between low and high, inclusive.
random_sample([size]) Return random floats in the half-open interval [0.0, 1.0).
random([size]) Return random floats in the half-open interval [0.0, 1.0).
ranf([size]) Return random floats in the half-open interval [0.0, 1.0).
sample([size]) Return random floats in the half-open interval [0.0, 1.0).
choice(a[, size, replace, p]) Generates a random sample from a given 1-D array
bytes(length) Return random bytes.
##### 1.1、numpy.random.random

numpy.random.random(size=None)：返回范围在半开区间[0.0, 1.0) 上的浮点数

random_sample、random、ranf、sample用法相同：

random_sample_type = type(np.random.random_sample())
print(random_sample_type)

random_sample = np.random.random_sample((5))#与random_sample(5,)相同
print(random_sample)

random_sample = np.random.random_sample((5,))
print(random_sample)

random_sample = np.random.random_sample((3,2))
print(random_sample)

# Three-by-two array of random numbers from [-5, 0):
random_sample_result = 5 * np.random.random_sample((3, 2)) - 5
print(random_sample_result)

<class 'float'>

[ 0.58452144  0.17618506  0.95080302  0.66095854  0.34928887]

[ 0.39012758  0.27384807  0.30607608  0.46398196  0.88590116]

[[ 0.12028886  0.57902902]
[ 0.87015091  0.1462187 ]
[ 0.43734193  0.09571964]]

[[-4.94102089 -1.79261502]
[-4.34365906 -3.16519113]
[-1.75801587 -1.88706362]]

##### 1.2、numpy.random.rand 生成随机浮点数

numpy.random.rand(d0, d1, ..., dn)

random_rand_type = type(np.random.rand())
print(random_rand_type)

random_rand = np.random.rand(5)#与random_rand(5,)相同
print(random_rand)

random_rand = np.random.rand(5,)
print(random_rand)

random_rand = np.random.rand(3,2)
print(random_rand)

# Three-by-two array of random numbers from [-5, 0):
random_rand_result = 5 * np.random.rand(3, 2) - 5
print(random_rand_result)

<class 'float'>

[ 0.26487271  0.0281932   0.8042671   0.2643821   0.33199909]

[ 0.91690691  0.3412498   0.11569359  0.53687716  0.19945599]

[[ 0.09106275  0.64573293]
[ 0.04541494  0.04964684]
[ 0.24620085  0.81908924]]

[[-1.70500552 -1.19790261]
[-2.56920771 -1.32168807]
[-4.77324942 -2.59249556]]

##### 1.3、numpy.random.randint 产生随机整数
numpy.random.randint(low, high=None, size=None, dtype='l')


1、随机生成[low,high)范围内的整数

random_randint = np.random.randint(3, 6, size=10)
print(random_randint)

[3 4 4 3 4 3 4 5 5 3]#说明3在范围内，而6不在范围内

2、指定size
size : 是int型 或者 是int型元组 , 默认是None返回单个整数

random_randint = np.random.randint(3, 10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=10)
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 5))
print(random_randint)
random_randint = np.random.randint(3, 10, size=(2, 2, 5))
print(random_randint)


9

[4 3 3 4 5 5 5 6 6 7]

[[3 3 4 3 8]
[7 6 4 6 9]]

[[[6 3 6 3 5]
[5 5 5 5 7]]
[[9 5 8 8 4]
[7 8 3 9 6]]]

3、 high 为None 时

random_randint = np.random.randint(3)
print(random_randint)

random_randint = np.random.randint(3,size=10)
print(random_randint)

2
[2 2 0 0 1 0 1 0 0 1]

##### 1.4、numpy.random.random_integers
numpy.random.random_integers(low, high=None, size=None)

random_integers = np.random.random_integers(3, 6, size=10)
print(random_integers)

random_integers = np.random.random_integers(3,size=10)
print(random_integers)

[4 4 6 3 6 6 3 3 6 4]
[2 1 2 2 2 3 3 2 1 2]
##### 1.5、numpy.random.randn

numpy.random.randn(d0, d1, …, dn)：生成一个浮点数或N维浮点数组，取数范围：正态分布的随机样本数。

randn = np.random.randn()
print(randn)

1.387157144507402

#Two-by-four array of samples from N(3, 6.25):
randn = 2.5 * np.random.randn(2, 4) + 3
print(randn)

[[ 6.03858058  4.47342334 -1.37679171  2.20495446]
[ 5.70048472  1.28674501 -1.06387771  3.38788724]]

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120