0% found this document useful (0 votes)
3 views6 pages

Untitled Document

The document outlines a simulation of coin tosses, divided into three parts: Part A simulates the number of tosses required to get heads, Part B analyzes the frequency of heads and tails using a biased coin, and Part C examines the distribution of heads in 10 biased coin tosses. Each part includes Python code for generating results and visualizations using matplotlib. The results provide insights into the behavior of coin tosses under different conditions.

Uploaded by

kentbaguio2234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Untitled Document

The document outlines a simulation of coin tosses, divided into three parts: Part A simulates the number of tosses required to get heads, Part B analyzes the frequency of heads and tails using a biased coin, and Part C examines the distribution of heads in 10 biased coin tosses. Each part includes Python code for generating results and visualizations using matplotlib. The results provide insights into the behavior of coin tosses under different conditions.

Uploaded by

kentbaguio2234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Kent RG A.

Baguio BSIT II-A


ACTIVITY #3

Part A: Distribution of Tosses to get heads


PART B:Frequency of Heads and Tails (Biased Coin)
Part C: Distribution of heads in 10 Biased Coin Toss
#Part A:

import numpy as np
import matplotlib.pyplot as plt

trials = 1000
outcomes = []

for _ in range(trials):
toss_attempts = 0
while True:
result = np.random.randint(0, 2)
toss_attempts += 1
if result == 1:
break
outcomes.append(toss_attempts)

least_tosses = np.min(outcomes)
most_tosses = np.max(outcomes)

least_freq = outcomes.count(least_tosses)
most_freq = outcomes.count(most_tosses)

least_percentage = (least_freq / trials) * 100


most_percentage = (most_freq / trials) * 100

print(f"Results of Coin Toss Simulation")


print(f"Fewest tosses: {least_tosses}")
print(f"Most tosses: {most_tosses}")
print(f"Least toss frequency: {least_freq} ({least_percentage:.2f}%)")
print(f"Most toss frequency: {most_freq} ({most_percentage:.2f}%)")

# Visualize the toss distribution


plt.hist(outcomes, bins=range(1, most_tosses + 2), color='purple', edgecolor='black', align='left')
plt.xlabel('Toss Count')
plt.ylabel('Occurrence')
plt.title('Tosses Needed to Get Heads')
plt.xticks(range(1, most_tosses + 1))
plt.show()
#Part B:
import numpy as np
import matplotlib.pyplot as plt

# Part B: Simulate 1000 tosses of a biased coin

num_tosses = 1000

coin_toss_results = np.random.choice(['Heads', 'Tails'], size=num_tosses, p=[2/3, 1/3])

heads_count = np.sum(coin_toss_results == 'Heads')


tails_count = np.sum(coin_toss_results == 'Tails')

print("\nPart B: Biased Coin Tosses (P(Heads) = 2/3)")


print(f"Number of Heads: {heads_count}")
print(f"Number of Tails: {tails_count}")

outcomes = ['Heads', 'Tails']


frequencies = [heads_count, tails_count]

plt.bar(outcomes, frequencies, color=['green', 'orange'], edgecolor='black')


plt.xlabel('Coin Side')
plt.ylabel('Frequency')
plt.title('Part B: Frequency of Heads and Tails (Biased Coin)')
plt.show()

#Part C:
import random as rd
import numpy as np
import matplotlib.pyplot as plt

n_tosses = 10
trials = 1000

num_heads = np.zeros(trials, dtype=int)


for i in range(trials):
heads_count = 0
for _ in range(n_tosses):
if rd.random() < 2/3:
heads_count += 1
num_heads[i] = heads_count

min_heads = int(np.min(num_heads))
max_heads = int(np.max(num_heads))

frequency = np.zeros(max_heads - min_heads + 1, dtype=int)


percentage = np.zeros(max_heads - min_heads + 1)

for heads in range(min_heads, max_heads + 1):


count = np.sum(num_heads == heads)
frequency[heads - min_heads] = count
percentage[heads - min_heads] = (count / trials) * 100

print("\nHeads Count | Frequency | Percentage")


for heads in range(min_heads, max_heads + 1):
print(f"{heads:11d} | {frequency[heads - min_heads]:9d} | {percentage[heads -
min_heads]:9.2f}%")

plt.plot(range(min_heads, max_heads + 1), percentage, color='blue', marker='o', linestyle='-',


label="Percentage")
plt.xlabel("Number of Heads")
plt.ylabel("Percentage")
plt.title(f"Percentage Distribution of Heads in {n_tosses} Tosses")
plt.grid(linestyle='--', alpha=0.7)
plt.legend()
plt.show()

You might also like