Thursday, September 28, 2023

# Winning The Lottery! Automatically :)

Howdy! Ok, I’m not guaranteeing you’ll win the lottery every time, or even ever, but I’ll show you here to generate automatic lottery numbers…for Mega Millions, Power Ball, Hit 5, and Lotto. This time, I’m using Python (a favorite among data scientists). Remember, I am NOT a programmer, but I’m a computer scientist. Difference? A programmer knows a specific language and goes deep into syntax, I don’t. A computer scientist understands algorithm, logic, architecture regardless of a specific language syntax (which always change…by smart humans) but can pick up any syntax by reference as needed, without memorization. My memory is bad ๐

Okay, so I was playing with Python mainly due to my certification program on AI and Data Science. We had to use it extensively. Yes, I got 6 certifications successfully, thank you ๐ But I was also curious to play with it afterward to make it do some mundane work. See, I rarely buy lottery tix, but when I do, I never pick the same numbers from a list. I always do quick pick…which the little machine generates (either runs Windows CE or Linux or some variant) using the same logic as I show you here.

I share the full source code below (you’ll obviously need a Python compiler to run this and you can get them anywhere on the Net for free) but first, I wanted to explain the logic because I’m not teaching any language syntax here, rather the logic of coding. It’s way more fun.

The Logic

First we create “sets” in Python. It’s like an array in most other languages (but Python makes it really easy to work with such beasts, manipulating them, outputting them, graphing them, etc. You can look that up in your own leisure).

Because each of the lotteries have different rules, I lazily create one list for each. For example, Mega Millions takes 5 numbers from 1 to 70 inclusive and a quick pick number that’s 1 to 25 inclusive. Lotto takes 6 numbers from 1 to 49 inclusive and doesn’t use any concept of quick pick. And so on. The source code below in gray boxes are comments and I put in the explanation for each block below.

On each set, I set the range according to the lottery game rules. Then I pick a random number (I love randoms!) from the set and repeat it as many times as necessary. For example, I need to pick 5 numbers from the list for Mega Millions. In most other languages, it’s several lines of code, but for busy data scientists Python makes it really simple…with 1 line of code. To pick 5 random numbers from a list, we only have to write:

`mypicks=random.sample(thelist, 5)`

To use the random function, we have to include the correct library where it’s defined. In Python, we do this with “import” statement (same idea as “include” in C/C++). The seed method of the random object (everything appears to be an object in Python much like in C++) takes a number of your choice to start the random math computation. You can read up more about seed() online and how it affects the math.

That’s it! Then I simply print out the results on screen with a simple print() function. So easy ๐ I intentionally didn’t optimize the code for brevity (there’s room). So, do it and take the challenge below.

Full source code below. Why an image (because you need to work at it by at least re-typing and trust me as you type each stroke will make you think about what and why :))

Take The Challengeย

Can you use 1 list instead of 4 and extract from the same list the 4 outputs? Can you optimize the code?

By The Way
If you do win lottery using this, won’t you share some wealth with me? ๐