随机数在计算机应用中十分常见
Python通过random库提供各种伪随机数
基本可以用于除加密解密算法外的大多数工程应用
(1)相同种子会产生相同的随机数
(2)如果不设置随机种子,以系统当前时间为默认值
from random import *
seed(10)
print(random())
seed(10)
print(random())
0.5714025946899135
0.5714025946899135
print(random()) #不设置deed,以系统当前时间为默认值,所以每次产生的都不同
0.4288890546751146
——产生[a, b]之间的随机整数(其中a和b都可以取到,左闭右闭区间)
from random import *
numbers = [randint(1,10) for i in range(10)]
numbers
[10, 1, 4, 8, 8, 5, 3, 1, 9, 8]
——产生[0, a)之间的随机整数,(不包含a,左闭右开区间)
from random import *
numbers = [randrange(10) for i in range(10)]
numbers
[5, 1, 3, 5, 0, 6, 2, 9, 5, 6]
——产生[a, b)之间以setp为步长的随机整数
from random import *
numbers = [randrange(0, 10, 2) for i in range(10)]
numbers
[6, 4, 4, 6, 2, 4, 4, 2, 6, 2]
——产生[0.0, 1.0)之间的随机浮点数
from random import *
numbers = [random() for i in range(10)]
numbers
[0.9693881604049188,
0.613326820546709,
0.0442606328646209,
0.004055144158407464,
0.13397252704913387,
0.941002271395834,
0.3028605620290723,
0.3661456016604264,
0.8981962445391883,
0.31436380495645067]
——产生[a, b]之间的随机浮点数
from random import *
numbers = [uniform(2.1, 3.5) for i in range(10)]
numbers
[2.8685750576173676,
2.710443340673771,
2.190991846577591,
2.9183647159827024,
3.281695056726663,
2.318986485742369,
2.414018556160458,
2.678018290800777,
2.1516948166820806,
2.7952448980631672]
——从目标序列类型中随机返回一个元素
from random import *
choice(['win', 'lose', 'draw'])
'draw'
from random import *
choice("python")
'n'
——对序列类型进行k次重复采样,可设置权重,权重小的说明被取到的概率更小
from random import *
choices(['win', 'lose', 'draw'], k=5)
['win', 'draw', 'win', 'lose', 'draw']
from random import *
choices(['win', 'lose', 'draw'], [4,4,2], k=10) # 这里的[4,4,2]就是每个元素分别对应的权重
['draw', 'win', 'draw', 'lose', 'lose', 'draw', 'win', 'win', 'draw', 'lose']
——将序列类型中元素随机排列,返回打乱后的序列
from random import *
numbers = ["one", "two", "three", "four"]
shuffle(numbers)
numbers
['two', 'four', 'one', 'three']
——从pop类型中随机选取k个元素,以列表类型返回,如果k大于所有元素的个数,则报错
from random import *
sample([10, 20, 30, 40, 50], k=3)
[20, 30, 10]
——生产一个符合高斯分布的随机数,mean–均值,std–标准差
from random import *
number = gauss(0, 1)
number
0.6331522345532208
多生成几个
import matplotlib.pyplot as plt
res = [gauss(0, 1) for i in range(100000)]
plt.hist(res, bins=1000)
plt.show()
import random
def red_packet(total, num):
for i in range(1, num):
per = random.uniform(0.01, total/(num-i+1)*2) # 保证每个人获得红包的期望是total/num
total = total - per
print("第{}位红包金额: {:.2f}元".format(i, per))
else:
print("第{}位红包金额: {:.2f}元".format(num, total))
red_packet(10, 5)
第1位红包金额: 1.85元
第2位红包金额: 3.90元
第3位红包金额: 0.41元
第4位红包金额: 3.30元
第5位红包金额: 0.54元
import random
import numpy as np
def red_packet(total, num):
ls = []
for i in range(1, num):
per = round(random.uniform(0.01, total/(num-i+1)*2), 2) # 保证每个人获得红包的期望是total/num
ls.append(per)
total = total - per
else:
ls.append(total)
return ls
# 重复发十万次红包,统计每个位置的平均值(约等于期望)
res = []
for i in range(100000):
ls = red_packet(10,5)
res.append(ls)
res = np.array(res)
print(res[:10])
np.mean(res, axis=0)
[[1.71 1.57 0.36 1.25 5.11]
[1.96 0.85 1.46 3.29 2.44]
[3.34 0.27 1.9 0.64 3.85]
[1.99 1.08 3.86 1.69 1.38]
[1.56 1.47 0.66 4.09 2.22]
[0.57 0.44 1.87 5.81 1.31]
[0.47 1.41 3.97 1.28 2.87]
[2.65 1.82 1.22 2.02 2.29]
[3.16 1.2 0.3 3.66 1.68]
[2.43 0.16 0.11 0.79 6.51]]
array([1.9991849, 2.0055725, 2.0018144, 2.0022472, 1.991181 ])
import random
import string
print(string.digits)
print(string.ascii_letters)
s=string.digits + string.ascii_letters
v=random.sample(s,4)
print(v)
print(''.join(v))
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
['n', 'Q', '4', '7']
nQ47