A Monte Carlo Simulation to Draw 3 same Color Balls Without Replaced From A Bucket With 3 Red Balls...

本文探讨了从包含3个红球和3个绿球的桶中,不放回地抽取3个同色球的概率。通过实施蒙特卡洛模拟,进行大量试验来估算这一概率,提供了Python代码实现,并分享了实验结果。

You have a bucket with 3 red balls and 3 green balls. Assume that once you draw a ball out of the bucket, you don't replace it. What is the probability of drawing 3 balls of the same color?

Write a Monte Carlo simulation to solve the above problem. Feel free to write a helper function if you wish.

 

def noReplacementSimulation(numTrials):
    '''
    Runs numTrials trials of a Monte Carlo simulation
    of drawing 3 balls out of a bucket containing
    3 red and 3 green balls. Balls are not replaced once
    drawn. Returns the a decimal - the fraction of times 3 
    balls of the same color were drawn.
    '''
    # Your code here
    sameColorNum = 0.0 
    for i in range(numTrials):
       redBallNum , buleBallNum = 0.0, 0.0
       balls = [0, 0, 0, 1, 1, 1]
       for k in range(3):
           draw = random.choice(balls)
           balls.remove(draw)
           if draw == 0:
               redBallNum += 1
           else:
               buleBallNum += 1

       if redBallNum == 3 or buleBallNum == 3:
           sameColorNum += 1
    return sameColorNum / numTrials
noReplacementSimulation(100000)

  0.10043

转载于:https://www.cnblogs.com/wbloger/p/10066155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值