Yes, I know that it is impossible to predict lottery results. But if the results would be predictable? Of course, I know they are not, but just hypothetically, for a play with Perceptron Neural Network and prediction algorithms, I want to show you that in 256 😉 Lines of code you can play with prediction thanks to the EnCog 3.4 library. I recently bought 2 of Jeff Heaton’s books about neural networks and C# and his library. That inspired me to play with prediction, forecast, trends, classifications, regression, and more… I do not want to bring you too many details, and I want to share that 256 lines of code. To make it work in Visual Studio, you have to just download by NuGet encog-dotnet-core 3.4 library and include it in the console application with that code. Then the last thing to prepare is the recent results of the euro jackpot lottery, like last year, and save that as the TXT file. But please remember, predicted values are correct in math, but it is impossible to predict truly random lottery results, which is Power Ball, but who can stop me from trying ;-). One more thing: good results can take some time, and good deep parameters, too high takes too long, and too low cannot find results, so play carefully. Here you have a portion of the program output.
Try 01: (16 22 27 34 41 + 4 8) 16:100% 22:100% 27:100% 34:100% 41:100% 4:100% 8:100% Try 02: (12 16 17 22 27 29 34 39 41 44 + 4 6 7 8) 12:50% 16:50% 17:50% 22:50% 27:50% 29:50% 34:50% 39:50% 41:50% 44:50% 4:50% 6:50% 7:50% 8:50% Try 03: (12 13 14 16 17 22 24 27 29 34 39 40 41 44 45 + 3 4 6 7 8) 12:33,33% 13:33,33% 14:33,33% 16:33,33% 17:33,33% 22:33,33% 24:33,33% 27:33,33% 29:33,33% 34:33,33% 39:33,33% 40:33,33% 41:33,33% 44:33,33% 45:33,33% 7:66,67% 3:33,33% 4:33,33% 6:33,33% 8:33,33% Try 04: (8 12 13 14 16 17 18 22 24 27 29 32 34 37 39 40 41 44 45 + 7 8) 44:50% 8:25% 12:25% 13:25% 14:25% 16:25% 17:25% 18:25% 22:25% 24:25% 27:25% 29:25% 32:25% 34:25% 37:25% 39:25% 40:25% 41:25% 45:25% 7:50% 8:50% 3:25% 4:25% 6:25% 9:25% Try 05: (8 10 12 13 14 15 16 17 18 22 24 27 29 32 34 35 37 39 40 41 44 45 + 7 8) 12:40% 39:40% 44:40% 8:20% 10:20% 13:20% 14:20% 15:20% 16:20% 17:20% 18:20% 22:20% 24:20% 27:20% 29:20% 32:20% 34:20% 35:20% 37:20% 40:20% 41:20% 45:20% 7:60% 8:40% 3:20% 4:20% 5:20% 6:20% 9:20% Try 06: (8 10 12 13 14 15 16 17 18 21 22 24 27 29 32 34 35 37 39 40 41 44 45 46 47 48 + 7 8) 12:33,33% 15:33,33% 39:33,33% 44:33,33% 8:16,67% 10:16,67% 13:16,67% 14:16,67% 16:16,67% 17:16,67% 18:16,67% 21:16,67% 22:16,67% 24:16,67% 27:16,67% 29:16,67% 32:16,67% 34:16,67% 35:16,67% 37:16,67% 40:16,67% 41:16,67% 45:16,67% 46:16,67% 47:16,67% 48:16,67% 7:66,67% 8:50% 3:16,67% 4:16,67% 5:16,67% 6:16,67% 9:16,67% Try 07: (12 15 16 39 44 + 7 8) 12:28,57% 15:28,57% 16:28,57% 39:28,57% 44:28,57% 8:14,29% 10:14,29% 11:14,29% 13:14,29% 14:14,29% 17:14,29% 18:14,29% 21:14,29% 22:14,29% 24:14,29% 27:14,29% 29:14,29% 30:14,29% 32:14,29% 34:14,29% 35:14,29% 37:14,29% 38:14,29% 40:14,29% 41:14,29% 43:14,29% 45:14,29% 46:14,29% 47:14,29% 48:14,29% 7:57,14% 8:42,86% 6:28,57% 9:28,57% 3:14,29% 4:14,29% 5:14,29%
As you can see “Try 07” founded exactly 7 numbers, but I think, you should experiment with the program source code and execution by yourself. Thanks for reading!
namespace EuroJackpotPredictor { using System; using System.Collections.Generic; using System.IO; using System.Linq; using Encog.Neural.Networks; using Encog.ML.Data.Basic; using Encog.Neural.Networks.Layers; using Encog.Engine.Network.Activation; using Encog.Neural.Networks.Training.Propagation.Resilient; class Result { public int V1 { get; private set; } public int V2 { get; private set; } public int V3 { get; private set; } public int V4 { get; private set; } public int V5 { get; private set; } public int V6 { get; private set; } public int V7 { get; private set; } public Result( int v1, int v2, int v3, int v4, int v5, int v6, int v7) { V1 = v1; V2 = v2; V3 = v3; V4 = v4; V5 = v5; V6 = v6; V7 = v7; } public Result(double[] values) { V1 = (int)Math.Round(values[0]); V2 = (int)Math.Round(values[1]); V3 = (int)Math.Round(values[2]); V4 = (int)Math.Round(values[3]); V5 = (int)Math.Round(values[4]); V6 = (int)Math.Round(values[5]); V7 = (int)Math.Round(values[6]); } public bool IsValid() { return V1 >= 1 && V1 <= 50 && V2 >= 1 && V2 <= 50 && V3 >= 1 && V3 <= 50 && V4 >= 1 && V4 <= 50 && V5 >= 1 && V5 <= 50 && V6 >= 1 && V6 <= 10 && V7 >= 1 && V7 <= 10 && V1 != V2 && V1 != V3 && V1 != V4 && V1 != V5 && V2 != V3 && V2 != V4 && V2 != V5 && V3 != V4 && V3 != V5 && V4 != V5 && V6 != V7; } public bool IsOut() { return !(V1 >= 1 && V1 <= 50 && V2 >= 1 && V2 <= 50 && V3 >= 1 && V3 <= 50 && V4 >= 1 && V4 <= 50 && V5 >= 1 && V5 <= 50 && V6 >= 1 && V6 <= 10 && V7 >= 1 && V7 <= 10); } public override string ToString() { return string.Format( "{0},{1},{2},{3},{4},{5},{6}", V1, V2, V3, V4, V5, V6, V7); } } class ListResults : List<Result> { } class Program { static void Main(string[] args) { var count = 0; var countMax = args.Length == 1 ? int.Parse(args[0]) : 27; var fileDB = @"C:\Projects\PredictorSandbox\db\ejpdb.txt"; try { ListResults dbl = null; if (CreateDatabases(fileDB, out dbl)) { var deep = countMax; var network = new BasicNetwork(); network.AddLayer( new BasicLayer(null, true, 7 * deep)); network.AddLayer( new BasicLayer( new ActivationSigmoid(), true, 5 * 7 * deep)); network.AddLayer( new BasicLayer( new ActivationSigmoid(), true, 5 * 7 * deep)); network.AddLayer( new BasicLayer(new ActivationLinear(), true, 7)); network.Structure.FinalizeStructure(); var learningInput = new double[deep][]; for (int i = 0; i < deep; ++i) { learningInput[i] = new double[deep * 7]; for (int j = 0, k = 0; j < deep; ++j) { var idx = 2 * deep - i - j; var data = dbl[idx]; learningInput[i][k++] = data.V1; learningInput[i][k++] = data.V2; learningInput[i][k++] = data.V3; learningInput[i][k++] = data.V4; learningInput[i][k++] = data.V5; learningInput[i][k++] = data.V6; learningInput[i][k++] = data.V7; } } var learningOutput = new double[deep][]; for (int i = 0; i < deep; ++i) { var idx = deep - 1 - i; var data = dbl[idx]; learningOutput[i] = new double[7] { data.V1, data.V2, data.V3, data.V4, data.V5, data.V6, data.V7, }; } var trainingSet = new BasicMLDataSet( learningInput, learningOutput); var train = new ResilientPropagation( network, trainingSet); train.NumThreads = Environment.ProcessorCount; START: System.Threading.Thread.Sleep(new Random().Next(10, 100)); network.Reset(); RETRY: var step = 0; do { train.Iteration(); ++step; } while (train.Error > (0.0001 * countMax) && step < countMax); var passedCount = 0; for (var i = 0; i < deep; ++i) { var should = new Result(learningOutput[i]); var inputn = new BasicMLData(7 * deep); Array.Copy( learningInput[i], inputn.Data, inputn.Data.Length); var comput = new Result( ((BasicMLData)network. Compute(inputn)).Data); var passed = should.ToString() == comput.ToString(); if (passed) { ++passedCount; } } var input = new BasicMLData(7 * deep); for (int i = 0, k = 0; i < deep; ++i) { var idx = deep - 1 - i; var data = dbl[idx]; input.Data[k++] = data.V1; input.Data[k++] = data.V2; input.Data[k++] = data.V3; input.Data[k++] = data.V4; input.Data[k++] = data.V5; input.Data[k++] = data.V6; input.Data[k++] = data.V7; } var perfect = dbl[0]; var predict = new Result( ((BasicMLData)network.Compute(input)).Data); Console.ResetColor(); if (predict.IsOut()) goto START; if (passedCount < (deep * 9d / 10) || !predict.IsValid()) goto RETRY; if (PrintPredicts(predict, ++count, --countMax)) goto START; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } static bool CreateDatabases( string fileDB, out ListResults dbl) { dbl = new ListResults(); using (var reader = File.OpenText(fileDB)) { var line = string.Empty; var separator = new string[] { " " }; while ((line = reader.ReadLine()) != null) { var values = line.Split(separator, StringSplitOptions.None); var res = new Result( int.Parse(values[2]), int.Parse(values[3]), int.Parse(values[4]), int.Parse(values[5]), int.Parse(values[6]), int.Parse(values[7]), int.Parse(values[8])); dbl.Add(res); } } return true; } static IDictionary<int, int> vs = new Dictionary<int, int>(50); static IDictionary<int, int> cs = new Dictionary<int, int>(10); static bool PrintPredicts(Result r, int count = 1, int countMax = 0) { if (!vs.ContainsKey(r.V1)) vs.Add(r.V1, 1); else vs[r.V1]++; if (!vs.ContainsKey(r.V2)) vs.Add(r.V2, 1); else vs[r.V2]++; if (!vs.ContainsKey(r.V3)) vs.Add(r.V3, 1); else vs[r.V3]++; if (!vs.ContainsKey(r.V4)) vs.Add(r.V4, 1); else vs[r.V4]++; if (!vs.ContainsKey(r.V5)) vs.Add(r.V5, 1); else vs[r.V5]++; if (!cs.ContainsKey(r.V6)) cs.Add(r.V6, 1); else cs[r.V6]++; if (!cs.ContainsKey(r.V7)) cs.Add(r.V7, 1); else cs[r.V7]++; var vss = from p in vs orderby p.Value descending, p.Key ascending select string.Format("{0}:{1}%", p.Key, Math.Round(100m*p.Value/count,2)); var vtp = (from p in vs orderby p.Value descending select p).Take(5).Last().Value; var css = from p in cs orderby p.Value descending, p.Key ascending select string.Format("{0}:{1}%", p.Key, Math.Round(100m*p.Value/count,2)); var ctp = (from p in cs orderby p.Value descending select p).Take(2).Last().Value; Console.WriteLine("Try {0}: ({1} + {2})", count.ToString().PadLeft(2, '0'), string.Join(" ", from p in vs where p.Value >= vtp orderby p.Key ascending select p.Key), string.Join(" ", from p in cs where p.Value >= ctp orderby p.Key ascending select p.Key)); Console.WriteLine(string.Join(" ", vss) + "\n" + string.Join(" ", css)); return countMax > 0; } } }
p ;).
First of all I would like to congratulate you on the idea.
I found your codes very cool, I am using your codes to do with a gaming system here in Brazil, I would like to know if you can get the txt file with the euro jackpot lottery results, for me to base.
Thanks again and congratulations.
hugs
and good luck
Danilo
Hi Danilo, you can replace call to load text file by calling SQL Server database table -- as I did, works perfect.
Sergei
Please make an instruction youtube video on how to import the NuGet encog libraries, the lottery draw history format txt and show how to compile the source code on VisualStudio without errors. Please place a download link of the source code. Is there multiple GPU sli crossfire support?
I think that ask needs about 8 coffees, about 1 days of work :D. Still interested? You got a point? :P.
Dan,
1. If you are using Visual Studio, you should know how to import NuGet libraries, it is very simple; if not sure -- search Google.
2. Judging by the code, the text file may look like below, i.e. 5 numbers between 1 and 50 and 2 numbers between 1 and 10:
8,12,31,39,49, 1, 8
1,10,11,22,50, 2, 10
But personally -- I prefer using a database, in my case I’ve been using local SQL Server, it is easier to maintain.
To get data from database use code:
static bool LoadDatabase(out ListResults dbl)
{
dbl = new ListResults();
string connString = @”Data Source=localhost\SERGEI;Initial Catalog=Lotteries;Integrated Security=True;Pooling=False”;
string sql = @”select Num1, Num2, Num3, Num4, Num5, PB1, PB2 from Lotteries.dbo.Powerball order by Draw desc”;
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Result res = new Result(
int.Parse(dr[0].ToString()),
int.Parse(dr[1].ToString()),
int.Parse(dr[2].ToString()),
int.Parse(dr[3].ToString()),
int.Parse(dr[4].ToString()),
int.Parse(dr[5].ToString()),
int.Parse(dr[6].ToString())
);
dbl.Add(res);
}
}
}
dbl.Reverse();
return true;
}
3. The code above is good and compiles without errors -- just copy it it to your newly created CONSOLE application in Visual Studio.