import random # 放回抽样:在整数区间[1,9]中有放回地抽取5次,求刚好抽出“1,2,3,4,5”的概率: # 记录放回抽样: def noted(): note = [] count = 0# 用于计算抽样次数 for each in range(1,6): while True: pick = random.randint(1, 10)# 按顺序找,当第一位满足要求(如: # 第一个元素已经取到‘1’了,就放在 # 列表中,记录下来。 count += 1 if pick == each: note.append(pick) break else: continue print(note) print(count) noted() # 不记录放回抽样(试图一次性得出[1,2,3,4,5]: def no_note(): count = 0# 用于计算抽样次数 while True: a = random.randint(1,10)# 取1-9的随机数 b = random.randint(1,10) c = random.randint(1,10) d = random.randint(1,10) e = random.randint(1,10) f = random.randint(1,10) g = random.randint(1,10) h = random.randint(1,10) j = random.randint(1,10) count += 1 result = [a,b,c,d,e] if result == [1,2,3,4,5]: print(result) print(count) break no_note()
python:记录性放回抽取与无记录放回抽取实验代码
最新推荐文章于 2024-08-04 20:27:41 发布