python:记录性放回抽取与无记录放回抽取实验代码

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值