码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-12-04 20:02:50      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:with   div   pre   for   mes   dba   function   drawing   raw   

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

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

标签:with   div   pre   for   mes   dba   function   drawing   raw   

原文地址:https://www.cnblogs.com/wbloger/p/10066155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!