英文:
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) <= 20) or ((min(draw) > 20 and max(draw) < 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 <= 10:
# we'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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论