Strange Coincidence

Lottery Winner

What would TV viewers think if the lottery lady announced that the numbers drawn from a bucket with 49 balls were 1, 2, 3, 4, 5, and 6? Since the probability of getting all six numbers right is about 1:14 million and there are 44 combinations of consecutive lottery number combinations (1, 2, 3, 4, 5, 6 through 44, 45, 46, 47, 48, 49), the chance of a straight in the lottery is about 1:318,000 – this means that the incredible event would occur relatively quickly with a fast draw generator.

Listing 4 shows an automatic drawing machine in the lotto_draw() function. From 49 numbered balls in the numbers list, it draws six random numbers and then removes them to prevent double draws. Since it takes a significant amount of compute time to remove an element from a Python list and move up the remaining elements to close the gap, the function swaps the value of the selected element with the last element in the list and reduces the list length size by one – much faster!

Listing 4


01 #!/usr/bin/env python3
02 import random
04 def lotto_draw():
05   total   = 49
06   draws   = 6
07   numbers = list(range(1,total+1))
08   size    = total
09   result  = []
11   for _ in range(draws):
12     idx = random.randrange(size)
13     result.append(numbers[idx])
14     numbers[idx] = numbers[size-1]
15     size -= 1
17   return sorted(result)
19 def is_consecutive(draw):
20   prev    = ""
21   for number in draw:
22     if prev < 0:
23       prev=number
24     elif prev + 1 == number:
25       prev = number
26     else:
27       return False
28   return True
30 count = 0
31 while True:
32   count += 1
33   draw=lotto_draw()
34   if is_consecutive(draw):
35       print("%d: %s" % (count, str(draw)))
36       break

Following this algorithm, lotto_draw() returns a sorted list of six randomly selected balls. The main program starting in line 30 uses is_consecutive() to check whether the drawn numbers each differ only by one from their predecessor. If this is the case, line 35 prints the number of draws in count and the lucky numbers that led to the termination. Figure 3 shows that this sometimes occurs after 30,000 passes; sometimes, however, it takes more than 800,000 – purely random, but within the calculated probability.

Figure 3: The lottery generator determines the number of draws until it finds a curious outcome.

Python Tricks [3] by Dan Bader is recommended for implementing this and other cool Python tricks. It shows a multitude of everyday programming tasks with elegant Python solutions. It is perfectly suited for users of other programming languages (like Perl!) who are mainly interested in converting typical idioms into clean Python and don't want to start with Adam and Eve and "Hello World."


  1. Malkiel, Burton G. A Random Walk down Wall Street. Norton & Company, 2016:
  2. Listings for this article:
  3. Bader, Dan. Python Tricks. Dan Bader, 2017:

The Author

Mike Schilli works as a software engineer in the San Francisco Bay area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you email him at he will gladly answer any questions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Making Money With Free Software

    A competition to design the new 5 euro commemorative coin, set by the Dutch Ministry of Finance, has been won by Stani Michiels, the Belgian artist and free software developer. His winning design, developed entirely with free software, has now been realized and is a legal coin in Holland.

  • ReportLab and Panda3D

    A game of bingo illustrates how to use the ReportLab toolkit and Panda3D real-time 3D engine.

  • KDE Plasma

    We take a peek at how to create your own plasmoids for the latest KDE desktop, giving you the power to build the perfect active desktop environment.

  • JavaScript Alternatives

    JavaScript is the stuff of which many interactive web clients is made, but it comes with a fair amount of historical ballast. The creators of four alternative scripting languages seek to ditch the ballast.

  • Magic Circle

    Inkscape's extensions add many useful features. Here's how to write your own.

comments powered by Disqus