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评论100阅读模式
英文:

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.

  1. random.seed(42)
  2. total = 0
  3. trials = 10000
  4. for _ in range(trials):
  5. draw = random.sample(range(1,31),5)
  6. if (max(draw) <= 20) or ((min(draw) > 20 and max(draw) < 30)):
  7. total += 1
  8. 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.

  1. random.seed(42)
  2. total = 0
  3. trials = 10000
  4. for _ in range(trials):
  5. draw = random.sample(range(1,31),5)
  6. if (max(draw) &lt;= 20) or ((min(draw) &gt; 20 and max(draw) &lt; 30)):
  7. total += 1
  8. total/trials

答案1

得分: 1

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

  1. import random
  2. random.seed(42)
  3. total = 0
  4. trials = 10000
  5. for _ in range(trials):
  6. red = 0
  7. green = 0
  8. draw = random.sample(range(1, 31), 5)
  9. for m in draw:
  10. if m <= 10:
  11. # 我们假设1-10号弹珠是红色的,11-30号弹珠是绿色的
  12. red += 1
  13. else:
  14. green += 1
  15. if red == 2 and green == 3:
  16. total += 1
  17. 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.

  1. import random
  2. random.seed(42)
  3. total = 0
  4. trials = 10000
  5. for _ in range(trials):
  6. red = 0
  7. green = 0
  8. draw = random.sample(range(1, 31), 5)
  9. for m in draw:
  10. if m &lt;= 10:
  11. # we&#39;ll say marbles 1-10 are red, 11-30 are green
  12. red += 1
  13. else:
  14. green += 1
  15. if red == 2 and green == 3:
  16. total += 1
  17. 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:

确定