# import the library numpy as np
# import the library matplotlib.pyplt as plt
import numpy as np
import matplotlib.pyplot as plt
# 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
array1= np.random.randint (low = 1, high= 50, size= 50)
print (array1)
plt.hist (array1, 5)
# generate 10000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
# compare with previous histogram
array2= np.random.randint (low = 1, high= 50, size= 1000)
print (array2)
plt.hist (array2, 5)
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram
z= np.random.uniform(low= 1, high=100, size = 1000)
plt.hist (z, 10)
# 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)
y = 50 + x * (5)
plt.hist (y, 10)
# 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
plt.hist (y, 10, density= True)
# import random and set seed
import random
random.seed (123456)
# Simulate drawing a single ball; each ball has a number from from 1 to 50
random.randint (1,50)
# Simulate drawing 100 balls and keep track of the number of balls
# that have a number less than 25
x = random.randint (1, 50)
n = 100
less_list= []
for i in range (0, n) :
x = random.randint (1, 50)
if x < 25:
less_list.append (random.randint (1, 50))
print (less_list)
print (f'out of {n} simulations {len(less_list)} random numbers were less than 25 ')
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
x = random.randint (1, 50)
n = 10000
less_list= []
for i in range (0, n) :
x = random.randint (1, 50)
if x < 25:
less_list.append (random.randint (1, 50))
d= (len(less_list))
print (d)
print (d/n)
print (f'the discrete probability of drawing a ball with a number less than 25 out of {n} simulations is {d/n}')
# 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 draw (n_times) :
outcomes = ['true', 'false']
n_true = 0; n_false = 0
for i in range (0, n_times) :
toss= random.randint (1,50)
if (toss < 25) :
n_true = n_true +1
print ('true')
else :
n_false = n_false + 1
print ('false')
return ( [n_true, n_false])
draw (1)