rand、randn、randi区别及用法
程序员文章站
2022-07-15 09:54:11
...
①random() 方法返回随机生成的一个实数,它在[0,1)范围内,不接受参数:
import random
print(random.random())#random 0.03563083745692497
②rand()方法随机样本位于[0, 1)中;参数(行,列)
import numpy as np
print("rand",np.random.rand(2,3))#rand [[0.25755196 0.81740437 0.79738575] [0.02873379 0.22991188 0.49516755]]
③randn从标准正态分布中返回一个或多个样本值;参数(行,列)
import numpy as np
print("randn",np.random.randn(2,3))#randn [[ 0.43310204 0.71676044 1.79460972][ 0.74348343 -0.52125996 1.07966401]]
④randint生成在半开半闭区间[low,high)上离散均匀分布的整数值;若high=None,则取值区间变为[0,low);size(行,列)
import numpy as np
import random
print("randit",np.random.randint(2,3,size=(1,2)))
print("randit2",random.randint(2,3))#randit [[2 2]]
#randit2 3
⑤np.random.seed())#使每次随机生成数一样,同numpy.random.RandomState()
import numpy as np
np.random.seed(2)#使每次随机生成数一样,同numpy.random.RandomState()
print("rand1",np.random.rand(2,3))
np.random.seed(2)
print("rand2",np.random.rand(2,3))
np.random.seed(6)#使每次随机生成数一样,同numpy.random.RandomState()
print("rand3",np.random.rand(2,3))
#rand1 [[0.4359949 0.02592623 0.54966248]
[0.43532239 0.4203678 0.33033482]]
rand2 [[0.4359949 0.02592623 0.54966248]
[0.43532239 0.4203678 0.33033482]]
rand3 [[0.89286015 0.33197981 0.82122912]
[0.04169663 0.10765668 0.59505206]]
上一篇: linux系统C语言实现域名解析功能
下一篇: 求一个整数存储在内存中的二进制中1的个数