随机数
生成随机数的主要函数:
import numpy as np
a=np.random.randint(0,10,10)#范围内的整数
print(a)
b=np.random.rand(10)#0到1的均匀分布
print(b)
c=np.random.randn(10)#标准正态分布
print(c)
d=np.random.normal(0,1,10)#生成指定正态分布
print(d)
e=np.random.random(10)#0到1的均匀分布
print(e)
f=np.random.ranf(10)#0到1的均匀分布
print(f)
g=np.random.uniform(-1,1,10)#指定均匀分布
print(g)
结果:
[2 2 0 9 9 2 5 2 5 9]
[0.43125551 0.61911646 0.09975441 0.13259641 0.40647513 0.04753234
0.52430223 0.08715624 0.88670744 0.50735008]
[ 0.00102429 0.33390838 0.63419928 -0.16225999 0.47162638 0.7289909
0.52563158 0.57049756 -0.22573231 0.16318548]
[ 1.96218715 1.08124505 1.38049178 -0.06828116 -1.07376452 1.41280129
-0.13680681 -0.90300261 -1.50940365 -2.06887085]
[0.46655881 0.10953279 0.55521648 0.90533316 0.24668374 0.88065066
0.61667896 0.9985584 0.37646195 0.1637208 ]
[2.63561937e-01 9.62893220e-01 2.07655124e-01 8.52387530e-01
5.14239629e-01 3.36797304e-01 7.65584834e-01 4.89905208e-04
9.41093128e-01 2.71334659e-01]
[ 0.06521078 -0.40595698 -0.24852623 0.41549426 0.58020432 0.96362697
-0.6170675 -0.28085888 0.3961527 0.48918203]
随机种子的设置
np.random.seed(0)
当然seed不一定为0