# import the library numpy as np
# import the library matplotlib.pyplt as plt
import numpy as np
import matplotlib.pyplot as plt
import random
# set a seed for your calculations so that they are reproducible
np.random.seed(12345)
# generate 50 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
jack = np.random.randint(low = 1, high = 50, size = 50)
plt.hist (jack, 5)
# generate 10000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
# compare with previous histogram
blue = np.random.randint (low = 1, high = 50, size = 10000)
plt.hist (blue, 5)
#The first plot looks cleaner because there are less numbers to plot
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram
x = np.random.uniform(size = 1000)
x = 1 + (100-10)*x
plt.hist (x, 5)
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a frequency histogram
#
x = np.random.normal(size = 1000)
x = 50 + (5)*x
plt.hist (x, 5)
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a density histogram; compare with frequency
# histogram
x = np.random.normal(size = 1000)
x = 50 + (5)*x
plt.hist (x, 5, density=True)
# these graphs look very similar in their view but the difference i see is
# that the y values in the density histogram have gotten extremely smaller
# import random and set seed
import random
random.seed(12345)
# Simulate drawing a single ball; each ball has a number from from 1 to 50
print (f"The ball chosen at random is numbered {random.randint(1,50)}")
# Simulate drawing 100 balls and keep track of the number of balls
# that have a number less than 25
def ball_picked (n_times) :
outcomes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
under = 0
over = 0
for i in range (0, n_times) :
pick = random.choice (outcomes)
if (pick < 25) :
under = under + 1
else :
over = over + 1
return ([under, over])
ball_picked (100)
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
n_times = 10000
picks = ball_picked (n_times)
prob_under = float (picks[0]) / float (n_times)
print (f"The discrete probability for picking a ball less than 25 is {prob_under}")
# Now suppose you are playing a game where you draw a ball. You win if you get a number
# <25 and lose otherwise. Write a function which draws a single ball and returns True
# if the number is <25 and false if it is >= 25
# Test out your function
def ball_picked (n_times) :
outcomes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
for i in range (0, n_times) :
pick = random.choice (outcomes)
if (pick < 25) :
return True
else :
return False
ball_picked (1)