Skip to content

Instantly share code, notes, and snippets.

@premithk
Created March 20, 2023 07:48
Show Gist options
  • Save premithk/13ebba86b9e043eb20c525178d3b683a to your computer and use it in GitHub Desktop.
Save premithk/13ebba86b9e043eb20c525178d3b683a to your computer and use it in GitHub Desktop.
import random
import numpy as np
TRIALS = 100000
URN_A = 'red', 'red', 'red' , 'red' , 'black', 'black', 'black'
URN_B = 'red', 'red', 'red' , 'red' , 'red', 'black', 'black', 'black', 'black'
URN_C = 'red', 'red', 'red' , 'red' , 'black', 'black', 'black', 'black'
success = 0
for i in range(TRIALS):
ballFromA = random.choice(URN_A)
ballFromB = random.choice(URN_B)
ballFromC = random.choice(URN_C)
selected_balls = [ballFromA, ballFromB, ballFromC]
if selected_balls.count('red') == 2:
success += 1
print("Probability from calculation =", np.round(17/42, 2))
print("Probability from monte carlo simulation =", np.round(success/TRIALS,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment