Problem 1:
# 1. Simulate tossing a fair coin 1000 times
import random
heads = sum(random.choice(['H', 'T']) == 'H' for _ in range(1000))
tails = 1000 - heads
print(f"Heads: {heads}, Tails: {tails}")
Problem 2:
# 2. Simulate rolling a six-sided die 1000 times
rolls = [random.randint(1, 6) for _ in range(1000)]
prob_six = rolls.count(6) / 1000
print(f"Probability of rolling a 6: {prob_six}")
Problem 3:
# 3. Birthday problem simulation
def has_duplicates(birthdays):
return len(birthdays) != len(set(birthdays))
matches = sum(has_duplicates([random.randint(1, 365) for _ in range(23)]) for _ in
range(1000))
prob = matches / 1000
print(f"Probability of shared birthday: {prob}")
Problem 4:
# 4. Drawing 2 red balls without replacement from 3 red and 2 blue
from itertools import combinations
bag = ['R', 'R', 'R', 'B', 'B']
red_red = [pair for pair in combinations(bag, 2) if pair == ('R', 'R')]
prob = len(red_red) / len(list(combinations(bag, 2)))
print(f"Probability of 2 red balls: {prob}")
Problem 5:
# 5. Rolling two dice 5000 times to find probability of sum 7
count = sum((random.randint(1,6) + random.randint(1,6)) == 7 for _ in range(5000))
print(f"Probability of sum 7: {count / 5000}")
Problem 6:
# 6. Probability from sample space
def probability(event, sample_space):
return len(event) / len(sample_space)
sample_space = {1, 2, 3, 4, 5, 6}
event = {2, 4, 6}
print(f"Probability: {probability(event, sample_space)}")
Problem 7:
# 7. Compute probability of getting exactly k heads in n coin tosses
from math import comb
def prob_k_heads(n, k):
return comb(n, k) * (0.5 ** n)
print(prob_k_heads(10, 5))
Problem 8:
# 8. Plotting distribution of number of heads in 50 coin tosses
import matplotlib.pyplot as plt
results = [sum(random.choice(['H', 'T']) == 'H' for _ in range(50)) for _ in
range(1000)]
plt.hist(results, bins=range(51), edgecolor='black')
plt.title("Distribution of Heads in 50 Tosses")
plt.xlabel("Number of Heads")
plt.ylabel("Frequency")
plt.show()
Problem 9:
# 9. Probability of a draw
P_R = 0.40
P_B = 0.50
P_D = 1 - (P_R + P_B)
print(f"Probability of a draw: {P_D}")
Problem 10:
# 10. Conditional probability given temperature range (example data)
data = [('High', True), ('Low', False), ('Medium', True), ('High', False)]
filtered = [r for t, r in data if t == 'High']
print(f"Conditional probability of rain given High temperature:
{sum(filtered)/len(filtered)}")
Problem 11:
# 11. Bayes' Theorem
def bayes(p_b_given_a, p_a, p_b):
return (p_b_given_a * p_a) / p_b
print(bayes(0.8, 0.1, 0.2))
Problem 12:
# 12. Conditional probability with dictionary
rain_data = {'High': (30, 50), 'Low': (10, 50)}
range_ = 'High'
rain, total = rain_data[range_]
print(f"Probability of rain given {range_}: {rain/total}")
Problem 13:
# 13. Spam Filter Analyzer using Bayes' Theorem
P_T = 0.1
P_F_given_T = 0.95
P_F_given_not_T = 0.05
P_not_T = 0.9
P_F = P_F_given_T * P_T + P_F_given_not_T * P_not_T
P_T_given_F = (P_F_given_T * P_T) / P_F
print(f"P(T|F): {P_T_given_F}")
Problem 14:
# 14. Simulate discrete PMF
pmf = {1: 0.2, 2: 0.5, 3: 0.3}
outcomes = random.choices(list(pmf.keys()), weights=pmf.values(), k=1000)
expected_value = sum(outcomes) / 1000
print(f"Expected value: {expected_value}")
Problem 15:
# 15. Biased 10-sided die
biased_die = [1,2,3,4,5,6,7,8,9,10]
weights = [0.05]*9 + [0.55]
samples = random.choices(biased_die, weights=weights, k=1000)
mean = sum(samples)/1000
variance = sum((x - mean) ** 2 for x in samples) / 1000
print(f"Expected Value: {mean}, Variance: {variance}")
Problem 16:
# 16. PageRank algorithm for 5 pages
import numpy as np
links = np.array([[0,1,1,0,0],[0,0,0,1,0],[0,1,0,1,1],[1,0,0,0,1],[0,0,1,0,0]])
rank = np.ones(5)/5
for _ in range(2):
rank = links.T @ rank
print(f"PageRank after 2 iterations: {rank}")
Problem 17:
# 17. Random surfer with damping
d = 0.85
n = 5
links = [[1,2],[3],[1,3,4],[0,4],[2]]
visits = [0]*n
page = 0
for _ in range(10000):
visits[page] += 1
if random.random() < d and links[page]:
page = random.choice(links[page])
else:
page = random.randint(0, n-1)
frequencies = [v/10000 for v in visits]
print(f"Visit frequencies: {frequencies}")