fork(6) download
  1. from __future__ import division, print_function
  2.  
  3. import random
  4.  
  5. balls = ['R', 'R', 'R', 'B']
  6.  
  7. two_red = 0
  8. one_red = 0
  9.  
  10. for i in xrange(10000):
  11. balls_taken = random.sample(balls, 2)
  12. looked_at = random.choice(balls_taken)
  13. if looked_at == 'B':
  14. continue
  15. elif balls_taken.count('R') == 2:
  16. two_red += 1
  17. else:
  18. one_red += 1
  19.  
  20. print('Times we took two red and looked at a red ball:', two_red)
  21. print('Times we took one red and looked at a red ball:', one_red)
  22. print('Probability the other ball is red:', two_red / (one_red + two_red))
Success #stdin #stdout 0.06s 10024KB
stdin
Standard input is empty
stdout
Times we took two red and looked at a red ball: 4980
Times we took one red and looked at a red ball: 2579
Probability the other ball is red: 0.658817303876