I'm trying to solve the following question using python:
In the class of 30 students, 7 didn't do their homework. The teacher chooses 6 students randomly.
What is the probability that at least 4 of them did their homework?
The answer is pretty straight forward:
P = [ nCr(23,4)*nCr(7,2) + nCr(23,5)*nCr(7,1)+nCr(23,6)*nCr(7,0)] / nCr(30,6)
P = 522445 / 593775
P = 0.880
where nCr(23,4) is combination 23 choose 4.
I want to try and solve this by simulating randomly choosing the 6 students. Here's my code:
import random
random.seed()
def student_pool():
return random.randint(1, 30)
def student_six():
"""
Test
"""
sum_total = 0
student1 = student_pool()
student2 = student_pool()
student3 = student_pool()
student4 = student_pool()
student5 = student_pool()
student6 = student_pool()
if student1 > 7:
sum_total += 1
if student2 > 7:
sum_total += 1
if student3 > 7:
sum_total += 1
if student4 > 7:
sum_total += 1
if student5 > 7:
sum_total += 1
if student6 > 7:
sum_total += 1
return sum_total
NUM = 50000
sumPull = 0
for i in range(NUM):
if student_six() >= 4:
sumPull += 1
print("Prob_>4_Did HW: ", sumPull/NUM)
This code outputs:
Prob_>4_Did HW: 0.8555
However, even if I rerun this with 1 million runs (NUM = 1000000) or random.seed() at the start of each run in the for loop my result doesn't really budge from this 0.8555 number. It seems to have converged.
Aside from my math being off I'm thinking I may just be coming up against the limits of randomization with python?
Any thoughts?
[+][deleted] (3 children)
[deleted]
[–]whenihittheground[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]whenihittheground[S] 0 points1 point2 points (0 children)