Showing posts with label Lotto. Show all posts
Showing posts with label Lotto. Show all posts

Monday, 23 June 2025

Lottery Numbers

 


Figure 1

Figure 1 shows the Lotto results for Saturday night 21st June 2025. What's unusual about the numbers is that seven out of the eight numbers are even. I was wondering as to the probability of this happening. To begin with there are 45 numbers to choose from: 22 are even and 23 are odd. See Figure 2.


Figure 2

Looking at the draw we see that the first number drawn was a 6 and this is an even number. The probability of drawing an even number is 22/45. The next three numbers were all even as well and the probabilities are therefore 21/44, 20/43 and 19/42. Then comes the only odd number, 1, and the probability of drawing an odd number is 23/41. The final three numbers are all even with probabilities of 18/40, 17/39 and 16/38. So the probability of this configuration:$$ \text{even, even, even, even. odd, even, even, even}$$is given by:$$\frac{22}{45} \times \frac{21}{44} \times \frac{20}{43} \times \frac{19}{42} \times \frac{23}{41} \times \frac{18}{40} \times \frac{17}{39} \times \frac{16}{38} \approx 0.0023$$However, we're not concerned with that particular order of even and odd numbers. We only want to determine the probability of seven even numbers and one odd number. The odd number could be in the first to eighth position. Each of these eight configurations has the same probability, around 0.0023, so we need to multiply by eight. This gives 0.01820 to five decimal places or a probability of around 1.82% and so$$\text{probability of one odd and seven even numbers } \approx 1.82 \text{%} $$Another way to approach the probability is via combinations. Let's see if we get the same result using this approach. There are \(^{22} \text{C}_{7}\) ways of choosing seven even numbers from the 22 available and \( ^23 \text{C}_1\) ways of choosing an odd number. With no restrictions, there are \( ^45 \text{C}_8 \) ways of choosing eight numbers from the 45 available. Thus:$$ \begin{align} \text{probability } &= \frac{^{22} \text{C}_{7} \times ^{23} \text{C}_1}{^{45} \text{C}_8}\\  \\[-1.5 ex] &=\frac{170544 \times 23}{215553195} \\[-0.5 ex]  \\ &\approx 0.01820 \\[-1.5 ex]  \\ &\approx 1.82\text{%} \end{align} $$So there is agreement. The probability that all the numbers are even would be given by:$$ \begin{align} \text{probability all even } &= \frac{ ^{22} \text{C}_8}{ ^{45} \text{C}_8}\\ \\[-1.5 ex] &\approx 0.00148 \\[-1.5 ex] \\ &\approx 0.148 \text{%} \end{align}$$This is considerably more unlikely than getting seven out of eight!

Monday, 10 June 2019

Lotto Loser

Figure 1
Figure 1 shows my Australian Gold Lotto results for Saturday night, the 1st June 2019. Once again, a prize of any sort eluded me but I was naturally struck by the fact that across my four games, I had all the winning numbers: 6, 9, 20, 25, 27 and 31 with 9 being chosen twice and each of the other numbers once.

Immediately, I wondered what was the probability of such an occurrence. Specifically, what were my chances of choosing all six winning numbers across four games but not have more than three winning numbers in any one game (prizes are awarded for four or more winning numbers). I'm going to ignore supplementary numbers in this analysis and only consider the red winning numbers.

I've pondered Lotto probabilities before in an earlier post titled Losing at Lotto (17th March 2018) in which I investigated the probability of not getting any numbers (red or blue) in four games. That turned out to be about 0.0066 or 0.66%. I made other posts about Lotto including Lotto Simulations (20th October 2018), The Mersenne Twister (20th January 2019 )and Oz Lotto (31st May 2017).

Anyway, back to problem under consideration. In no game can I have more than three winning numbers or else I'd win a prize. So a constant for each game is that I need to choose 3 losing numbers out of the 39 available. There are 39 x 38 x 37 ways of doing this. What I've done in Figure 2 is to show the range of minimum possible configurations (where there is no repetition of winning numbers across the four games). From those configurations I've worked out the remaining possibilities, given the constraints that there cannot be more than three winning numbers in any game and there cannot be a repeated number in any game.

Figure 2

We'll tackle each in turn:

Configuration 1, top left, multiplying rows: 

6 x 5 x 4 x 39 x 38 x 37
3 x 2 x 1 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37   

Configuration 2, top middle, multiplying rows: 

6 x 5 x 43 x 39 x 38 x 37 
4 x 3 x 43 x 39 x 38 x 37
2 x 1 x 43 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37   

Configuration 3, top right, multiplying rows: 

6 x 5 x 4 x 39 x 38 x 37
3 x 2 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37 
45 x 44 x 43 x 39 x 38 x 37 

Configuration 4, bottom left, multiplying rows:

6 x 5 x 43 x 39 x 38 x 37
4 x 3 x 43 x 39 x 38 x 37
2 x 44 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37

Configuration 5, bottom middle, multiplying rows:

6 x 5 x 4 x 39 x 38 x 37
3 x 44 x 43 x 39 x 38 x 37
2 x 44 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37   

Each product on each line is divided by \(^{45}p_5\) or 45 x 44 x 43 x 42 x 41 x 40 = 5,864,443,200 and all lines are multiplied together within a configuration. The probabilities for all configurations are then added because they are mutually exclusive. The results are shown in Figure 2 (a spreadsheet snapshot).

Figure 3
The chances work out to be slightly less than two in ten million (1.89), whereas the probability of getting all six numbers in one game is slightly more than one in ten million (1.23). To me, this result seems far too low. 

Another approach involves the use of combinations, which I tried initially but dismissed because the probability seemed far too high. However, I'll revisit that approach here and see what I come up with a second time around. The essential points in this approach are:

  • there are four games
  • there are 45C6 ways of choosing the six numbers in each game
  • up to three winning numbers (6C3) can be chosen in each game
  • conversely, there must be three losing numbers (38C3) in each game
  • all six winning numbers must be chosen across the four games

The configurations shown in Figure 3 can be reused but this time with combinations. See Figure 4 below.

Figure 4

Each of the line above needs to be divided by 45C6 to determine the probabilities which turn out to be:
  • 0.00638281628563513 for configuration 1 
  • 0.321868017603801 for configuration 2 
  • 0.0548922200564621 for configuration 3 
  • 0.472073092485574 for configuration 4 
  • 0.161017178832289 for configuration 5
  • 1.01623332526376 overall
Clearly something is terribly wrong as probability cannot exceed 1. Below is the SageMath code I used for the calculation:
C11=binomial(6,3) #6C3 x 39C3 C12=binomial(3,3) #3C3 x 39C3 C13=binomial(45,3) #45C3 x 39C3
C14=binomial(45,3) #45C3 x 39C3
C21=binomial(6,2)*binomial(43,1) #6C2 x 43C1 x 39C3
C22=binomial(4,2)*binomial(43,1) #4C2 x 43C1 x 39C3
C23=binomial(2,1)*binomial(43,1) #2C1 x 43C1 x 39C3 C24=binomial(45,3) #45C3 x 39C3 C31=binomial(6,3) #6C3 x 39C3
C32=binomial(3,2)*binomial(43,1) #3C2 x 43C1 x 39C3
C33=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C34=binomial(45,3) #45C3 x 39C3 C41=binomial(6,2)*binomial(43,1) #6C2 x 43C1 x 39C3
C42=binomial(4,2)*binomial(43,1) #4C2 x 43C1 x 39C3 C43=binomial(2,1)*binomial(44,2) #2C1 x 44C2 x 39C3
C44=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C51=binomial(6,3) #6C3 x 39C3 C52=binomial(3,1)*binomial(44,2) #3C1 x 44C2 x 39C3
C53=binomial(2,1)*binomial(44,2) #2C1 x 44C2 x 39C3 C54=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C1=n(C11*C12*C13*C14*(binomial(39,3)/binomial(45,6))^4)
C2=n(C21*C22*C23*C24*(binomial(39,3)/binomial(45,6))^4)
C3=n(C31*C32*C33*C34*(binomial(39,3)/binomial(45,6))^4)
C4=n(C41*C42*C43*C44*(binomial(39,3)/binomial(45,6))^4)
C5=n(C51*C52*C53*C54)*((binomial(39,3)/binomial(45,6))^4)
print C1, C2, C3, C4, C5
p=C1+C2+C3+C4+C5
print p
I'll need to return to this and try to resolve the problem. My first result is vanishingly small and my second is impossibly high!

Sunday, 20 January 2019

The Mersenne Twister

I stumbled upon the Mersenne Twister after investigating Pseudorandom Number Generators (PRNGs) stimulated by an article about a hacker who had made a speciality of exploiting weaknesses in the PRNGs incorporated into certain types of slot machines.

Here is an excerpt from the article:
In the course of reverse engineering Novomatic’s software, Alex encountered his first PRNG. He was instantly fascinated by the elegance of this sort of algorithm, which is designed to spew forth an endless series of results that appear impossible to forecast. It does this by taking an initial number, known as a seed, and then mashing it together with various hidden and shifting inputs—the time from a machine’s internal clock, for example. Writing such algorithms requires tremendous mathematical skill, since they’re supposed to produce an output that defies human comprehension; ideally, a PRNG should approximate the utter unpredictability of radioactive decay.
The intriguingly named Mersenne Twister is described thus by Wikipedia:
The Mersenne Twister is a pseudorandom number generator (PRNG). It is by far the most widely used general-purpose PRNG. Its name derives from the fact that its period length is chosen to be a Mersenne prime. 
The Mersenne Twister was developed in 1997 by Makoto Matsumoto and Takuji Nishimura. It was designed specifically to rectify most of the flaws found in older PRNGs. 
The most commonly used version of the Mersenne Twister algorithm is based on the Mersenne prime \(2^{19937}−1\). The standard implementation of that, MT19937, uses a 32-bit word length. 
The Mersenne Twister is the default PRNG for the following software systems: Microsoft Excel, GAUSS,  GLib, GNU Multiple Precision Arithmetic Library, GNU Octave, GNU Scientific Library, gretl, IDL, Julia,  CMU Common Lisp, Embeddable Common Lisp, Steel Bank Common Lisp, Maple, MATLAB, Free Pascal, PHP, Python, R, Ruby, SageMath, Scilab, Stata.
I don't want to delve too deeply into the how the Mersenne Twister actually generates its random numbers. However, the following article provides a simple overview of how it works.

How does the Mersenne Twister work?

Posted February 2016

Someone asked that question on reddit, and so I replied with a high level answer that should provide a clear enough view of the algorithm:

From a high level, here's what a PRNG is supposed to look like (DIAGRAM 1):


DIAGRAM 1

You start with a seed (if you re-use the same seed you will obtain the same random numbers), you initialise it into a state. Then, every time you want to obtain a random number, you transform that state with a one-way function \(g\). This is because you don't want people to find out the state out of the random output.

You want another random number? You first transform the state with a one way function
\(f\): this is because you don't want people who found out the state to be able to retrieve past states (forward secrecy). And then you use your function \(g\) again to output a random number.

Mersenne Twister (MT) is like that, except:
  • your first state is not used to output any random numbers
  • a state allows you to output not only one, but 624 random numbers (although this could be thought as one big random number)
  • the \(g\) function is reversible, it's not a one-way function, so MT it is not a cryptographically secure PRNG.
With more details, here's what MT looks like (DIAGRAM 2):


DIAGRAM 2

The \(f\) function is called "twist", the \(g\) function is called "temper". You can find out how each functions work by looking at the working code on the wikipedia page of MT.

*********************************

The slot machine hacking via knowledge of the PRNG within got me thinking about my own gambling on Lotto, a vice I indulge in weekly. How are the lotto numbers selected? Are there physical, numbered balls involved or is a PRNG used? There's no shortage of sites that will generate random numbers for you to enter in any lottery of your choosing but how are the actual winning numbers generated? Well it seems that the numbers are physically selected in Australian lotteries as shown in the following video:


However, Lucky Lotteries is different.


To quote from the website:
Lucky Lotteries is a raffle style jackpot game that guarantees over 10,000 prizes in every draw! Unlike other lottery games, each number is unique so there is no sharing of prizes. 
There are two Lucky Lotteries games – Super Jackpot and Mega Jackpot. 
Each game has a set amount of numbers per draw and once all available numbers are sold, the winning numbers are drawn using a random number generator.
So clearly a PRNG is used in certain lotteries. Of course, I use SageMath extensively and so it's of interest to know about what PRNG it uses. SageMath documentation explains that:
It is possible to select which random number generator drives the sampling as well as the seed. The default is the Mersenne twister. Also available are the RANDLXS algorithm and the Tausworthe generator (see the gsl reference manual for more details). These are all supposed to be simulation quality generators. For RANDLXS use rng = 'luxury' and for tausworth use rng = 'taus': 
sage: T = RealDistribution('gaussian', 1, rng = 'luxury', seed = 10)

Saturday, 20 October 2018

Lotto Simulations

I've written about Lotto before in this post (Oz Lotto) and another (Losing at Lotto). The current post is not about probabilities but simulation. How can we use SageMath to simulate a lottery draw? Well as it turns out it's not too difficult.


The key is to use the ZZ.random_element(lowerbound, upperbound+1) function as in the following code. The trick is not to allow duplicate numbers and that is the reason for the inclusion of Set. This function takes our lotto list which might look like this the list [20, 37, 6, 37, 1, 19, 16] and turns it, after the sort, into the ordered set {1, 6, 16, 19, 20, 37}.

Here is the code I developed. I'm really just a tyro at both Python and SageMath so there may well be much better ways to achieve my goals but this is what I came up with:
# Generate a set of numbers for lotto games
# let N represent how many numbers are required in the game
# let G represent the number of games
# let U represent the upper bound of numbers
G=8
N=7
U=40
for i in range(G):
    lotto=[]
    while len(Set(lotto))<N:
        lotto.append(ZZ.random_element(1,U+1))
    print sorted(Set(lotto))
The following is an example of the output:
[11, 23, 27, 30, 37, 38, 40]
[4, 7, 8, 15, 20, 29, 38]
[8, 9, 13, 14, 21, 28, 36]
[7, 11, 15, 21, 28, 33, 40]
[1, 15, 17, 18, 28, 33, 39]
[4, 7, 11, 14, 18, 29, 32]
[1, 10, 12, 16, 21, 31, 33]
[2, 7, 8, 12, 13, 19, 27]
How really random this output is I don't know. I'm sure SageMath has other ways to generate more truly random numbers but this is sufficient for the modest purposes of the exercise.

The code has the added advantage of being able to accommodate "fixed" numbers. For example, let's say we always want the numbers 23 and 32 included in the output. All that needs to be done is to start with lotto = [23, 32] instead of it being an empty list.

For example:
# Generate a set of numbers for lotto games
# let N represent how many numbers are required in the game
# let G represent the number of games
# let U represent the upper bound of numbers
G=8
N=7
U=40
for i in range(G):
    lotto=[23, 32]
    while len(Set(lotto))<N:
        lotto.append(ZZ.random_element(1,U+1))
    print sorted(Set(lotto))
Here an example of the output:
[10, 11, 21, 23, 32, 35, 37]
[5, 6, 17, 23, 31, 32, 39]
[6, 11, 17, 23, 31, 32, 39]
[3, 15, 18, 23, 26, 30, 32]
[2, 12, 13, 23, 27, 28, 32]
[7, 18, 23, 26, 32, 35, 38]
[2, 11, 17, 19, 23, 32, 33]
[3, 16, 23, 26, 27, 31, 32] 
Not that I expect it will make any difference to my chances of winning but I'll test this out in some coming Lotto games. Oddly enough what got me thinking about generating some random lotto numbers was this article titled JERRY AND MARGE GO LARGE in the Huffington Post. It's about an American couple who exploited a feature in their state's lottery to make millions of dollars. It's quite an interesting story.

As I've been doing I'll include a SageMathCell with the code so that anyone curious can experiment:

Saturday, 7 April 2018

The New Powerball

I received an email from the Lott today announcing the following changes to Powerball:
New Powerball Updates 
BIG JACKPOTS
There is an increased chance of big jackpots occurring more often.
 
MORE WINNERS
There will be more overall winners in every draw with the chance of winning an overall prize increasing from 1 in 78 to 1 in 44.
 
NEW 9TH PRIZE DIVISION
The Powerball prize structure will include an extra 9th prize division.
 
NUMBER UPDATES
The New Powerball game will feature 7 winning numbers drawn from a barrel of 35 balls (numbered 1-35). This was previously 6 numbers drawn from a barrel of 40 balls (numbered 1-40).
 
We’ve randomly generated replacements for numbers greater than 35 and added a new 7th number to your Favourite and Subscription entries.
There will be no changes to the Powerball. It will still be drawn from a barrel of 20 balls (numbered 1-20).
This claim of an increased chance of big jackpots occurring more often is perplexing because it seems that the number of possible combinations has increased, meaning the probability of winning has decreased (significantly as we can see below):$$ \text{BEFORE }\binom{40}{6} \times 20 = 76,767,600 \text{ <---> AFTER } \binom{35}{7} \times 20 = 134,490,400$$ The chance of winning, that is getting the correct combination of seven numbers and the powerball, is now almost halved.

Maybe I'm missing something but I can't see what it is. There may well be more overall winners with the introduction of a 9th prize division but I wonder if the provision of even more piddling minor prizes will actually entice punters. Even without knowing the precise probability, most punters will recognise intuitively that, with the powerball choices remaining the same, the chance of getting seven numbers correct out of 35 is going to be considerably less than getting six numbers out of 40. All punters have their eye of the big prize not on the increasing proliferation of minor prizes.

Saturday, 17 March 2018

Losing at Lotto

Figure 1
Each week, I play four games in the Saturday Night Gold Lotto and, though I seldom win a prize, I always manage to choose at least one winning number. For each game in this particular lottery, you must choose six numbers out of a total of 45 (numbered from 1 to 45) and, during the draw, six main numbers are chosen as well as two supplementary numbers (so there are eight "winning" numbers). If you select all six main numbers, you share in the first division prize of $4,000,000. The amount is divided among the total numbers of winners which on average is between four and five. Combinations of main numbers and supplementary numbers win lesser prizes. Tonight, I was surprised to see that I'd managed to select none of the eight winning numbers. I've attached a screenshot of my results (see Figure 1). This got me wondering what was the probability of doing this? It had to be fairly low because I couldn't recall it ever happening to me before.

To tackle the problem, let's consider a single game in which there are 37 "losing" numbers along with the eight "winning" numbers. How many ways can these 37 numbers be chosen if I am selecting six numbers at a time, disregarding the order in which they are chosen. There are \( ^{37}C_6 \)= 2516 ways to do this out of a total of \(^{45}C_6\) = 8815 possibilities. So the probability of choosing six losing numbers in a single game is 2516/8815 which is almost 0.29 or 29%. Thus for every 100 games I play, around 29 will result in my missing all of the winning numbers. However, it is highly unlikely that I'll "succeed" in doing this four times in a row because each game is independent of the other and so the probabilities are multiplied. Thus the fraction 2516/8815 is raised to the fourth power and this gives a probability of about 0.0066 or 0.66%. In other words, if I play 10,000 sets of four games each, I'll only miss all of the winning numbers about 66 times. Tonight was one of those times.

Below is shown the WolframAlpha calculation:


For an earlier post on the probability of winning at Oz Lotto, follow this link.

UPDATE: on Saturday March 6th 2021 I also managed to select no numbers out of four games. Thus it is almost three years since this last happened. Figure 2 shows a screenshot of the results.

Figure 2