1、np.random.seed()
每次运行代码时设置相同的seed,则每次生成的随机数也相同,如果不设置seed,则每次生成的随机数都会不一样。
关于seed()函数用法:
seed( ) 用于指定随机数生成时所用算法开始的整数值。
1.如果使用相同的seed( )值,则每次生成的随即数都相同;
2.如果不设置这个值,则系统根据时间来自己选择这个值,此时每次生成的随机数因时间差异而不同。
3.设置的seed()值仅一次有效
2、numpy.random.rand()
numpy.random.rand(d0,d1,…,dn)
- rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
- dn 表格每个维度
- 返回值为指定维度的array
np.random.rand(4,2)
array([[ 0.02173903, 0.44376568],
[ 0.25309942, 0.85259262],
[ 0.56465709, 0.95135013],
[ 0.14145746, 0.55389458]])
np.random.rand(4,3,2) # shape: 4*3*2
array([[[ 0.08256277, 0.11408276],
[ 0.11182496, 0.51452019],
[ 0.09731856, 0.18279204]],
[[ 0.74637005, 0.76065562],
[ 0.32060311, 0.69410458],
[ 0.28890543, 0.68532579]],
[[ 0.72110169, 0.52517524],
[ 0.32876607, 0.66632414],
[ 0.45762399, 0.49176764]],
[[ 0.73886671, 0.81877121],
[ 0.03984658, 0.99454548],
[ 0.18205926, 0.99637823]]])
3、numpy.random.randn()
numpy.random.randn(d0,d1,…,dn)
- randn函数返回一个或一组样本,具有标准正态分布。
- dn 表格每个维度
- 返回值为指定维度的array
np.random.randn() # 当没有参数时,返回单个数据
-1.1241580894939212
标准正态分布介绍
- 标准正态分布—-standard normal distribution
- 标准正态分布又称为u分布,是以0为均值、以1为标准差的正态分布,记为N(0,1)。
4、numpy.random.randint()
numpy.random.randint(low, high=None, size=None, dtype=’l’)
- 返回随机整数,范围区间为[low,high),包含low,不包含high
- 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
- high没有填写时,默认生成随机数的范围是[0,low)
np.random.randint(1,size=5) # 返回[0,1)之间的整数,所以只有0
array([0, 0, 0, 0, 0])
np.random.randint(1,5) # 返回1个[1,5)时间的随机整数
4
np.random.randint(-5,5,size=(2,2))
array([[ 2, -1],
[ 2, 0]])
仅供自己学习参考:https://blog.csdn.net/u012149181/article/details/78913167