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
lotto
01 #!/usr/bin/env python3 02 import random 03 04 def lotto_draw(): 05 total = 49 06 draws = 6 07 numbers = list(range(1,total+1)) 08 size = total 09 result = [] 10 11 for _ in range(draws): 12 idx = random.randrange(size) 13 result.append(numbers[idx]) 14 numbers[idx] = numbers[size-1] 15 size -= 1 16 17 return sorted(result) 18 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 29 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.
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."
Infos
- Malkiel, Burton G. A Random Walk down Wall Street. Norton & Company, 2016: https://www.amazon.com/Random-Walk-Down-Wall-Street-ebook/dp/B00QH9NTSI
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/212/
- Bader, Dan. Python Tricks. Dan Bader, 2017: https://dbader.org/products/python-tricks-book/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.