# import the library numpy as np
# import the library matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
# set a seed for your calculations so that they are reproducible
x=np.random.seed(123456)
# generate 50 random integers from 1 to 50 using random.randint() and make a
# frequency histogram with 5 bars
x=np.random.randint(low = 1, high = 50, size = 50)
print(x)
plt.hist(x, 5) #5 is the amount of bars/bins
# generate 10,000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram with 5 bars
# compare with previous histogram
y=np.random.randint(low = 1, high = 50, size = 10000)
print(y)
plt.hist(y, 5) #5 is the amount of bars/bins
#compare previous 2 histograms against each other with 5 bins
plt.hist([x, y], bins=5, color=['red', 'blue'], label=['x', 'y'])
plt.legend()
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram with 5 bars
x = np.random.uniform(size = 1000)
x = 1 + 99*x
print(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 with 5 bars
z = np.random.normal(size = 1000, loc = 50, scale = 5)
plt.hist(z, 5)
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a density histogram with 5 bars; compare with frequency
# histogram
# basically just copy the formula from previous question but add in density = True parameter!
w = np.random.normal(size = 1000, loc = 50, scale = 5)
plt.hist(w, 5, density = True)
# Compare the histograms from the previous 2 questions here!
# z = the normal histogram, w = the density histogram
plt.hist([z, w], bins=5, color=['red', 'blue'], label=['z', 'w'])
plt.legend()
# import random and set seed
import random
random.seed(1234)
# 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 # Number of balls being drawn
less_list = [] # Leave open so it can be filled as the simulation goes
for i in range (0, n):
x = random.randint(1, 50)
if x < 25:
less_list.append(random.randint(1,50))
print("Less_list =",less_list)
print(f"Out of {n} simulations, {len(less_list)} random numbers were less than 25") #len(less_list) gives the number of items in the list
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
# Discrete probability (D) = chances of the number of balls being <25 / the total number of balls
# Basically copy the basics from the last problem but change the necessary values
y = random.randint(1, 50)
n = 10000 # Number of balls being drawn
less_list = [] # Leave open so it can be filled as the simulation goes
for i in range (0, n):
y = random.randint(1, 50)
if y < 25:
less_list.append(random.randint(1,50))
D = (len(less_list)) # The number of balls with a value <25
print("D =", D)
print("D/n =", D/n)
print(f"The discrete probability that you will draw a ball with a number <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
for i in range (0, n):
pull = random.randint(1, 50)
if (pull < 25):
print("True, <25, win!")
else:
print("False, >=25, lose")