Using Artificial Intelligence Techniques And SQL To Optimize A Lottery Application

Artificial Intelligence or “AI” can be defined as the “ability of a system to learn from previous experience in an attempt to predict future events”. While this might seem a forlorn hope in making lottery predictions, let’s see if this might be possible.

The big problem in analysing lottery results is the large number of possible combinations. In a standard “40 x 6” ball game there are 3,838,380 lines to choose from.

Generating A Starting Point For A Lotto AI System

It makes sense to reduce the possible number pool before we implement any AI possibilities. I’ll use the “Prime” probability system which assumes there will be 2 primes, 1 odd and 3 even numbers in each winning line.

We’ll try to use AI to group the prime results into some kind of pattern when they do occur.

For example, the last 6 weeks results of NZ Lotto were:


The numbers in results 2,3,4 and 6 meet the prime system criteria so we only want to look at those lines. If we can establish a pattern within the prime results then we can greatly reduce our combinations for when the prime system numbers come up.

We’ll use the Structured Query Language, or SQL, to query the prime system database of all possible lottery combinations; a total 575,586 lines of 6 numbers.

The structure of the database is as follows:

Number types: prime,prime,odd,even,even,even
Column name: n1,n2,n3,n4,n5,n6
For example:

Using A SQL Query To Recognize The Lottery Pattern

What I’ll do is create a SQL query that selects each line of winning prime system numbers. We can then combine the queries in an “and/or” statement to cover the different scenarios.

In this way, we can learn the commonalities between the different results, just like a real life AI application.

Most of the prime results above contained at least one number divisible by 4. We can cover that scenario with this query:

n4%4=0 or n5%4=0 or n6%4=0

Records returned: 525,690

The percent symbol is a special code in SQL which gives us the remainder from dividing the two numbers.

Each prime line appears to have at least two numbers within 4 of each other. We’ll cover that possibility like this:


Records returned: 305,316

We can now combine the queries with the following command:

(n4%4=0 or n5%4=0 or n6%4=0)

The combined query returned 276,210 combinations, a reduction of around 40%, which is fairly impressive.

I noticed the first even number was always equal to or less than 14 and when I factored that parameter in, the combinations returned were 165,726.

In future weeks I’ll look at other common factors, for example excluding numbers from recent results.


This article explored the possibility of using AI techniques and SQL to reduce possible combinations. By analysing results that meet certain parameters it should be possible to reduce line numbers and improve our lottery chances.