Question: extend your Python simulation over multiple trials by embedding your code into a for loop over the number of trials desired, say 100, 1000 or
extend your Python simulation over multiple trials by embedding your code into a for loop over the number of trials desired, say 100, 1000 or 1000. Again, let the user specify the number of people in the room. Estimate the probability of having the same birthday as yours by counting the number of times a birthday match occurred, over the number of trials.
Heres my Code:
import random
nPeople = int(input("How many people are in the room: ")) birthday=random.randint(1,365)
nSuccess=0; nTrials=0;
while True:
strRun = str(input("Run Trial? y/n ")) if strRun.lower()=="y":
nTrials=nTrials+1
print("Running trial #: ",nTrials) roomBirthdays=[random.randint(1,365) for _ in range(nPeople)]
if birthday in roomBirthdays:
nSuccess=nSuccess+1
print("Number of Matches: {0}".format(nSuccess)) print("Probability of someone sharing your birthday for a room of {0}: {1:.2f}".format(nPeople,float(nSuccess)/nTrials)) elif strRun.lower()=="n":
break
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
