# 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
np.random.seed(123456)
# generate 50 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
randomintegers_50 = np.random.randint(1, 51, size=50)
plt.hist(randomintegers_50, bins=5, edgecolor='black')
plt.title("Random Integers (1-50) - 50 Samples")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
# generate 10000 random integers from 1 to 50 using random.randint() and make a
# frequency histogram
# compare with previous histogram
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
#previous histogram
ax1.hist(randomintegers_50, bins=5, edgecolor='black', alpha=0.5, label="50 Samples")
ax1.set_title("50 Random Integers (1-50)")
ax1.set_xlabel("Value")
ax1.set_ylabel("Frequency")
ax1.legend()
# Plot the histogram for 10,000 random integers
random_integers_10000 = np.random.randint(1, 51, size=10000)
ax2.hist(random_integers_10000, bins=5, edgecolor='black', alpha=0.5, label="10,000 Samples")
ax2.set_title("10,000 Random Integers (1-50)")
ax2.set_xlabel("Value")
ax2.set_ylabel("Frequency")
ax2.legend()
plt.show()
# generate 1000 floating point numbers uniformly distributed from 1 to 100 and make a
# frequency histogram
random_uniform = np.random.uniform(1, 100, size=1000)
plt.hist(random_uniform, bins=5, edgecolor='black')
plt.title("Random Uniform Distribution (1-100)")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
# generate 1000 floating point numbers normally distributed about a mean of 50
# with a standard deviation of 5 and make a frequency histogram
#
random_normal = np.random.normal(50, 5, size=1000)
plt.hist(random_normal, bins=5, edgecolor='black')
plt.title("Random Normal Distribution (mean=50, std=5)")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
# 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(random_normal, bins=5, edgecolor='black', density=True)
plt.title("Random Normal Distribution (mean=50, std=5) - Density Histogram")
plt.xlabel("Value")
plt.ylabel("Density")
plt.show()
# import random and set seed
import random
random.seed(123)
# Simulate drawing a single ball; each ball has a number from from 1 to 50
singledraw = random.randint(1, 50)
print("Single Draw:", singledraw)
# Simulate drawing 100 balls and keep track of the number of balls
# that have a number less than 25
count_less_than_25 = 0
for i in range(100):
ball_number = random.randint(1, 50)
if ball_number < 25:
count_less_than_25 += 1
# Calculate discrete probability that you will draw a ball with a number <25 using 10,000
# simulations
probability_simulation = count_less_than_25 / 100
theoretical_probability = 24 / 50 # Theoretical probability
print(f"Probability (Simulation): {probability_simulation}")
print(f"Theoretical Probability: {theoretical_probability}")
# 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_ball():
ball_number = random.randint(1, 50)
return ball_number < 25
# Test the function
result = draw_ball()
if result:
print("The ball has a number < 25")
else:
print("The ball does not have a number < 25")
import numpy as np
import matplotlib.pyplot as plt
# Data
data = [220, 223, 229, 231, 234, 236, 240, 242, 244, 248, 253, 259, 260, 263, 264]
# Define the bins for the histogram
bins = [220, 235, 250, 265]
# Create the density histogram
plt.hist(data, bins=bins, density=True, edgecolor='k', alpha=0.5)
# Add labels and a title
plt.xlabel("Foot Length (mm)")
plt.ylabel("Probability Density")
plt.title("Density Histogram")
# Show the histogram
plt.show()