title: Lottery probability Python code
body: <p>I'm working on a code to solve this problem:</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Here's my code:</p>
  
  <pre><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 &lt;= k &lt;= 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)
  </code></pre>
  
  <p>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. </p>