阐述python中几个产生随机数函数的区别

numpy中有一些常用的用来产生随机数的函数,下面就常用的几种进行介绍。

import numpy as np
import random
print(np.random.random())              #产生随机浮点数

print(np.random.rand())                #从标准正态分布中返回一个或多个样本值

print(np.random.randn())               #随机样本位于(0, 1)中

print(np.random.randint(20,30))        #用于生成一个指定范围内的随机符点数,两个参数其中
                                       #一个是上限,一个是下限。如果a > b,则生成随机数

print(random.randrange(0,101,2))       #从指定范围内,按指定基数递增的集合中
                                       #(随机选取0到100间的偶数) 

#测试seed()产生随机种子的作用,加上np.random.seed(1),只产生一个相同的随机数
i=0
while(i<4):
    np.random.seed(1)
    print(np.random.rand())
    i+=1
i=0
while(i<4):
    print(np.random.rand())
    i+=1
    
print("test over")
运行结果如下:
0.2161833234686963
0.5386873681551301
-0.21398377459667148
21
74
0.417022004702574
0.417022004702574
0.417022004702574
0.417022004702574
0.7203244934421581
0.00011437481734488664
0.30233257263183977
0.14675589081711304
test over

猜你喜欢

转载自blog.csdn.net/qq_41997920/article/details/88378051