Skip to content

Instantly share code, notes, and snippets.

@russelllim22
Last active November 3, 2022 02:53
Show Gist options
  • Save russelllim22/b8e3077f6a50816a7cba024a1c65c767 to your computer and use it in GitHub Desktop.
Save russelllim22/b8e3077f6a50816a7cba024a1c65c767 to your computer and use it in GitHub Desktop.
import random
import pandas as pd
def simulate(n):
trial_results = []
while True:
if len(trial_results) == n:
return trial_results
else:
cards = []
while True:
cards.append(random.choice(['H','S','W']))
# if I have at least 4 cards, check whether I have ['H','H','S','W']
if(len(cards)>=4 and
"S" in cards and
"W" in cards and
cards.count("H")>1 ):
# record the result (number of cards it took to win)
trial_results.append(len(cards))
break
trial_results = simulate(10000)
df = pd.DataFrame(trial_results, columns=['trial_results'])
pd.set_option("display.max_rows", 10)
print(df)
print("mean from 10000 trials:")
print(df['trial_results'].mean())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment