I am trying to write a code for class that identifies the probability that two or more people given can have the same birthday out of a group by running this experiment 10^6 times and then calculating the fraction of times during which two or more people had the same birthday. I attached the code below, but when I try and run it absolutely nothing happens. It gives no error message, it just stops working. Does anyone have an idea why?
-
import random
def calc_birthday_probability (num_people):
random.seed (2020) # Don't change this value
num_trials = 1000000
probability = 0
list1 = []
num_people = int(input())
repeats = 0
for i in range(0,num_trials+1):
for i in range (0,num_people+1):
list1.append(random.randint (1,3655))
for i in list1:
if list1.count(i)>1:
repeats +=1
i = i+1
i = i+1
prob = repeats//num_trials
probability = probability + prob
return probability
a = calc_birthday_probability(10)
print(a)
num_people = int(input()) Classic, you are already given the parameter in the function but you are retaking input for it. Your program simply waits for you to enter something.
Usually, a good trick when a python program does nothing despite it being expected to perform some kind of action is to check whether or not it is waiting for some kind of input :)
Edit #1: as @rkatkam noted, you are using the same loop variable (specifically, i ) for both of the for loops.
In all the for
loops in the function, you have used variable i
and so its scope is improper to define too.
Something like following should work, it worked for me:
def calc_birthday_probability (num_people):
random.seed (2020) # Don't change this value
num_trials = 1000000
probability = 0
list1 = []
repeats = 0
for i in range(0,num_trials+1):
for j in range (0,num_people+1):
list1.append(random.randint (1,3655))
for k in list1:
if list1.count(j)>1:
repeats +=1
k = k+1
prob = repeats//num_trials
probability = probability + prob
return probability
And some tips:
Try testing your code with smaller num_trials
initially till you find the results are accurate.
You have argument that accepts the value of num_people
and the function also has an input()
for the same.
When you trying to print output, print some other string as well along, to identify if the function finished its execution.
Simpler version of your code
import random
def calc_birthday_probability(number_of_people = 30, num_trials=1000):
dups_found = 0
for _ in range(num_trials):
birthdays = [random.randint (1,365) for _ in range(number_of_people)]
# set of birthdays which are duplicates
duplicates = set(x for x in birthdays if birthdays.count(x) > 1)
if len(duplicates) >= 1:
dups_found += 1 # increment since at least one duplicate
return number_of_people, dups_found/num_trials * 100
num_people = int(input("Number of people: "))
print(f'{calc_birthday_probability(num_people, 1000):.2f}%')
Test
Testing with only 1,000 trials since sufficient for results to compare with reference
for num_people in range(1, 52, 2):
print(f'{num_people} --> {calc_birthday_probability(num_people, 1000):.2f}%')
Output
1 --> 0.00%
3 --> 0.40%
5 --> 3.10%
7 --> 6.10%
9 --> 9.90%
11 --> 13.70%
13 --> 19.00%
15 --> 25.30%
17 --> 34.60%
19 --> 37.70%
21 --> 46.50%
23 --> 53.30%
25 --> 57.30%
27 --> 59.60%
29 --> 70.40%
31 --> 72.40%
33 --> 77.90%
35 --> 81.60%
37 --> 84.30%
39 --> 87.90%
41 --> 89.30%
43 --> 93.40%
45 --> 93.70%
47 --> 95.00%
49 --> 96.10%
51 --> 96.60%
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.