Lottery probability Python code
Question:
I’m working on a code to solve this problem:
You and your friends are in New York and are planning to go see a Broadway musical. Unfortunately, New York being New York, the tickets are just a tiny bit expensive. But one of the shows has a ticket lottery each night where impecunious people such as you have a chance to win the right to buy slightly less expensive tickets to good seats. The lottery operates as follows. First, everyone interested enters the lottery. Then, n lucky winners are drawn, and each of these is offered to buy up to t tickets.
Given the number of people p in your group (all of which entered the lottery) and the total number of people m that entered the lottery, what is the probability that you will be able to get tickets for your entire group? Assume that the n lucky winners are chosen uniformly at random from the m people that entered the lottery, and that each person can win at most once.
Here’s my code:
import math
def lottery():
m = int(raw_input('The number of people who entered the lottery: '))
n = int(raw_input('The number of winner drawn from the total: '))
t = int(raw_input('The number of tickets each winner can purchase: '))
p = int(raw_input('The number of people in your group: '))
def combinations(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
needed_wins = int(math.ceil(p/t))
others = m - p
loss = 0
for i in range(needed_wins):
loss += combinations(others, n-i) * combinations(p, i)
total = combinations(m, n)
prob = 1 - loss / total
print(prob)
I tried to run it but the result came out wrong. For example, if the combination is (100,10,2,1), the result should be 0.1; instead it returned 1. I really appreciate it if anyone can help me out here.
Answers:
In Python 2, when you divide two integers, you always get an integer result. Try adding this line to the top of the file, which will get you the new Python 3 behavior, where dividing ints produces floats:
from __future__ import division
import math
def lottery(m, n, t, p):
needed_wins = math.ceil(p/t)
others = m - p
loss = 0
for i in range(needed_wins):
comb_others = combinations(others, n-i)
comb_group = combinations(p, i)
loss += comb_others * comb_group
total = combinations(m, n)
prob = 1 - loss / total
return prob
def combinations(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
I’m working on a code to solve this problem:
You and your friends are in New York and are planning to go see a Broadway musical. Unfortunately, New York being New York, the tickets are just a tiny bit expensive. But one of the shows has a ticket lottery each night where impecunious people such as you have a chance to win the right to buy slightly less expensive tickets to good seats. The lottery operates as follows. First, everyone interested enters the lottery. Then, n lucky winners are drawn, and each of these is offered to buy up to t tickets.
Given the number of people p in your group (all of which entered the lottery) and the total number of people m that entered the lottery, what is the probability that you will be able to get tickets for your entire group? Assume that the n lucky winners are chosen uniformly at random from the m people that entered the lottery, and that each person can win at most once.
Here’s my code:
import math
def lottery():
m = int(raw_input('The number of people who entered the lottery: '))
n = int(raw_input('The number of winner drawn from the total: '))
t = int(raw_input('The number of tickets each winner can purchase: '))
p = int(raw_input('The number of people in your group: '))
def combinations(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in xrange(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
needed_wins = int(math.ceil(p/t))
others = m - p
loss = 0
for i in range(needed_wins):
loss += combinations(others, n-i) * combinations(p, i)
total = combinations(m, n)
prob = 1 - loss / total
print(prob)
I tried to run it but the result came out wrong. For example, if the combination is (100,10,2,1), the result should be 0.1; instead it returned 1. I really appreciate it if anyone can help me out here.
In Python 2, when you divide two integers, you always get an integer result. Try adding this line to the top of the file, which will get you the new Python 3 behavior, where dividing ints produces floats:
from __future__ import division
import math
def lottery(m, n, t, p):
needed_wins = math.ceil(p/t)
others = m - p
loss = 0
for i in range(needed_wins):
comb_others = combinations(others, n-i)
comb_group = combinations(p, i)
loss += comb_others * comb_group
total = combinations(m, n)
prob = 1 - loss / total
return prob
def combinations(n, k):
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0