参考:
https://docs.scipy.org/doc/numpy/reference/routines.random.html
https://blog.csdn.net/akadiao/article/details/78252840?locationNum=9&fps=1
https://blog.csdn.net/kancy110/article/details/69665164
####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
参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random.html#numpy.random.random
功能:返回范围在半开区间[0.0, 1.0) 上的浮点数
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)
打印:
[ 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 生成随机浮点数
参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.rand.html#numpy.random.rand
功能:返回范围在半开区间[0.0, 1.0) 上的浮点数
numpy.random.rand(d0, d1, ..., dn)
默认为生成一个随机的浮点数,范围是[0, 1),也可以通过参数size设置返回数据的size。
与numpy.random.random不同的是:numpy.random.random接收的元组,numpy.random.rand不是
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)
打印:
[ 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 产生随机整数
参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randint.html#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 时
那么结果的范围为 [0, low).
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
参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.random_integers.html#numpy.random.random_integers
numpy.random.random_integers(low, high=None, size=None)
生成一个整数或一个N维整数数组,取值范围:若high不为None,则取[low,high]之间随机整数,否则取[1,low]之间随机整数。
用法与random.randint相似,不同的是取值区间为闭区间[low, high],并且high为None时取值范围为范围为 [1, low].
原文:similar to randint, only for the closed interval [low, high], and 1 is the lowest value if high is omitted. In particular, this other one is the one to use to generate uniformly distributed discrete non-integers.
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
使用公式: s i g m a ∗ n p . r a n d o m . r a n d n ( . . . ) + m u sigma * np.random.randn(...) + mu sigma∗np.random.randn(...)+mu 从 $ N(\mu, \sigma^2)$获取样本
#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]]