Discussion 01

Farhana Zahir

4th June 2020

In [52]:
# Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import library
import random
import numpy as np
import matplotlib.pyplot as plt

Problem: What is the probability that we come up with an even sum if we roll 2 die 1000 times?

In [69]:
#Function to calculate probability
def roll_die(n):
    count=0
    score=[]
    for i in range(n):
        die1=random.randint(1,6)
        die2=random.randint(1,6)
        total=die1+die2
        score.append(total)
        
        if total%2==0:
            count+=1
    
    print('The probability is', count/n)
    
    #Plot the total score, bin set to unique values in score
    plt.hist(score, bins = 11)
    plt.xlabel('Sum of Die')
    plt.ylabel('No of observations')
    plt.title('Total scores for 1000 simulations')
    plt.xticks(np.unique(score))
        
In [70]:
#Run the function
roll_die(1000)
The probability is 0.495