A bag contains 10 red and 20 green marbles. Draw 5 without replacement. What is the probability of getting 2 red and 3 green?

huangapple go评论74阅读模式
英文:

A bag contains 10 red and 20 green marbles. Draw 5 without replacement. What is the probability of getting 2 red and 3 green?

问题

I know how to get the probability of drawing marbles all of the same color, but I don't know how to code it to get 2 red and 3 green.

    random.seed(42)
    
    total = 0
    trials = 10000
    
    for _ in range(trials):
        
        draw = random.sample(range(1,31),5)
        
        if (max(draw) <= 20) or ((min(draw) > 20 and max(draw) < 30)):
            total += 1
            
    total/trials
英文:

I know how to get the probability of drawing marbles all of the same color, but I don't know how to code it to get 2 red and 3 green.

random.seed(42)

total = 0
trials = 10000

for _ in range(trials):
    
    draw = random.sample(range(1,31),5)
    
    if (max(draw) &lt;= 20) or ((min(draw) &gt; 20 and max(draw) &lt; 30)):
        total += 1
        
total/trials

答案1

得分: 1

对于每次试验(每次抽取5颗弹珠),您需要记录红色弹珠和绿色弹珠的数量。如果恰好有2颗红色弹珠和3颗绿色弹珠,就增加您的 total 计数器。

import random

random.seed(42)

total = 0
trials = 10000

for _ in range(trials):

    red = 0
    green = 0

    draw = random.sample(range(1, 31), 5)
    for m in draw:
        if m <= 10:
            # 我们假设1-10号弹珠是红色的,11-30号弹珠是绿色的
            red += 1
        else:
            green += 1

    if red == 2 and green == 3:
        total += 1

print(total / trials)
英文:

For each trial (each time you draw 5 marbles) you need to keep count of how many are red and how many are green. If exactly 2 are red and 3 are green, increment your total counter.

import random

random.seed(42)

total = 0
trials = 10000

for _ in range(trials):

    red = 0
    green = 0

    draw = random.sample(range(1, 31), 5)
    for m in draw:
        if m &lt;= 10:
            # we&#39;ll say marbles 1-10 are red, 11-30 are green
            red += 1
        else:
            green += 1

    if red == 2 and green == 3:
        total += 1

print(total / trials)

huangapple
  • 本文由 发表于 2023年2月7日 04:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定