<jupyter_start><jupyter_text>Dice SimulationAlice wants to join her school's Probability Student Club. Membership dues are computed via one of two simple probabilistic games. The first game: roll a die repeatedly. Stop rolling once you get a five followed by a six. Your number of rolls is the amount you pay, in dollars. The second game: same, except that the stopping condition is a five followed by a five.Which of the two games should Alice elect to play? Does it even matter? Write a program to simulate the two games and calculate their expected value.<jupyter_code>def game_one(): 
    tosses = [] 
    tosses = [random.choice([1,2,3,4,5,6])] # initialize with a toss
    condition_met = True
    
    while condition_met: 
        roll_result = random.choice([1,2,3,4,5,6]) 
        tosses.append(roll_result)
        # tosses[-2] is the 2nd to last item in the list
        # remember, we just put in roll_result as the last item. 
        if tosses[-2] == 5 and roll_result == 6:    
            condition_met = False
    
    num_tosses = len(tosses) 
    return num_tosses, tosses[-15:] # give me the last 5 in tosses
game_one()
def game_one_simulation(): 
    results = [] 
    n_trials = 1000
    
    for i in range(n_trials): 
        num_tosses, _ = game_one() # we don't care about tosses[-5:] now
        results.append(num_tosses) 
    
    EV_game = sum(results)/len(results) 
    return EV_game
game_one_simulation()
def game_two(): 
    tosses = []
    tosses = [random.choice([1,2,3,4,5,6])]
    condition_met = True
    
    while condition_met: 
        roll_result = random.choice([1,2,3,4,5,6])
        tosses.append(roll_result)
        if tosses[-2] == 5 and roll_result == 5: 
            condition_met = False
            
    num_tosses = len(tosses)
    return num_tosses, tosses[-15:] 
game_two()
def game_two_simulation(): 
    results = [] 
    n_trials = 1000
    
    for i in range(n_trials): 
        num_tosses, _ = game_two()
        results.append(num_tosses) 
        
    EV_game = sum(results)/len(results)
    return EV_game
game_two_simulation()<jupyter_output><empty_output><jupyter_text>Now let's run the same analysis but let's collect all of the results.Ultimately we are going to run a histogram of the results and check to see if the central limit theorem holds here. It should look normally distributed because what we have is a distribution of the means.<jupyter_code>def game_two_avg_collector(): 
    m_trials = 250
    EV_collector = [] 
    
    for i in range(m_trials): 
        EV_i = game_two_simulation()
        EV_collector.append(EV_i) 
    
    return EV_collector 
# this will take a while to load.. 500 * 1000 trials = 500,000 passes 
five_five_values = game_two_avg_collector()
plt.hist(five_five_values, 20) # Cool, looks normally distributed.
# Let's find the average of X_values 
average = sum(five_five_values)/len(five_five_values)
print(average)<jupyter_output>42.06715999999998<jupyter_text>Cool, that looks normally distributed. Let's put them on the same histogram to get a better comparison..<jupyter_code>def game_one_avg_collector(): 
    m_trials = 250
    EV_collector = [] 
    
    for i in range(m_trials): 
        EV_i = game_one_simulation()
        EV_collector.append(EV_i) 
    
    return EV_collector
five_six_values = game_one_avg_collector()
# Put the two distributions on the same distribution plot. 
plt.hist(five_five_values, 20, alpha = 0.5, label='five five', color = 'g')
plt.hist(five_six_values, 20, alpha = 0.5, label ='five six', color = 'b')
plt.legend(loc='upper right')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08942865028981507, 'fraction_numerical': 0.025117306099917195, 'mean_word_length': 3.293838862559242, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '67587', 'license': 'MIT', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1164, 'n_words': 364, 'path': 'Dice Games/Dice Simulation.ipynb', 'repo_name': 'jasonyum/math-puzzles', 'size': '3623'}