System for Playing Texas Lottery "All or Nothing" Game

Published:

I am posting this C# program that I wrote in my spare time to this website for anybody here who is interested in seeing it. Feel free to use it withoutany copyright restraint. You can take it and use it to write own program and I don't mind or care. Just remember that there is no warranty or guarantee that the system works and that you use it at your own risk.

 

//---------------------------------------------------------------------------------
//
// AON.cs - All or Nothing Forecaster for the Texas Lottery. (c) 2020 LG Soft.
//
// Wheeling system based on the use of a genetic algorithm.
//
//---------------------------------------------------------------------------------
using System;
using System.IO;

//---------------------------------------------------------------------------------
// aon - main class
//---------------------------------------------------------------------------------
public class aon
{
public const int maxlines = 10000; // max lines in data
public const int maxfields = 50; // max fields in data
public const int ntrials = 5000; // number of genetic tr.
public const int nt = 5; // number of tickets g.
public int[,] data = new int[maxlines,maxfields]; // data to test
public int[] ticket = new int[12]; // one ticket
public int[] winningticket = new int[12]; // store of winner
public int[,] winningtickets = new int[nt,12]; // store of winners
public double maxscore = 0; // highest score in test
public double maxscoref = 0; // total score
public int count = 0; // number of lines
public int winnings = 0; // total winnings
public double winperticket = 0; // avg. win per ticket
public double twinperticket = 0; // total win avg. win
public bool debugflag = false; // when true, dumps var.
public bool debugflag2 = true;

public string outfile = "aon-outputfile.csv"; // output file name

//---------------------------------------------------------------------------------
// Console1 - class for writing output to screen and file
//---------------------------------------------------------------------------------
public class Console1
{
//---------------------------------------------------------------------------------
// WriteLine1 - to write to the screen and file
//---------------------------------------------------------------------------------
public static void WriteLine1(string str1)
{
Console.WriteLine(str1);
aon a = new aon();
StreamWriter sw = new StreamWriter(a.outfile,true);
sw.WriteLine(str1);
sw.Close();
}

//---------------------------------------------------------------------------------
// Write1 - to write to the screen and file
//---------------------------------------------------------------------------------
public static void Write1(string str1)
{
Console.Write(str1);
aon a = new aon();
StreamWriter sw2 = new StreamWriter(a.outfile,true);
sw2.Write(str1);
sw2.Close();
}

//---------------------------------------------------------------------------------
// WriteLine1 - to write to the screen and file
//---------------------------------------------------------------------------------
public static void WriteLine1()
{
Console.WriteLine();
aon a = new aon();
StreamWriter sw3 = new StreamWriter(a.outfile,true);
sw3.WriteLine();
sw3.Close();
}


}

//---------------------------------------------------------------------------------
// cleardata - to clear the data variables
//---------------------------------------------------------------------------------
public void cleardata()
{
int i1 = 0;
int i2 = 0;
for (i1 = 0; i1 < maxlines; i1++)
{
for (i2 = 0; i2 < maxfields; i2++)
{
data[i1,i2] = 0;
}
}
}

//---------------------------------------------------------------------------------
// loaddata - to load the data file for testing the theories of the genetic alg.
//---------------------------------------------------------------------------------
public void loaddata()
{
StreamReader sr = new StreamReader("data1.csv");
bool exitflag = false;
int i1 = 0;
string line1 = "";
//---------------------------------------------------------------------------------
// loop through the input file
//---------------------------------------------------------------------------------
while (!exitflag)
{
try
{
line1 = sr.ReadLine();
}
catch
{
exitflag = true;
}
try
{
i1 = line1.Length;
}
catch
{
exitflag = true;
}
if (!exitflag)
{
string[] fields = line1.Split(',');
int[] fieldsn = new int[fields.Length];
//---------------------------------------------------------------------------------
// parse the data into its fields
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < fields.Length; i1++)
{
try
{
fieldsn[i1] = int.Parse(fields[i1]);
if (debugflag)
{
Console1.Write1(fields[i1]+",");
}
}
catch
{
}
}
if (debugflag)
{
Console1.WriteLine1();
}
for (i1 = 0; i1 < 12; i1++)
{
data[count,i1] = fieldsn[i1+3];
}
count = count + 1;

}
}
sr.Close();
}

//---------------------------------------------------------------------------------
// geneticticket - to generate genetic ticket trials.
//---------------------------------------------------------------------------------
public void geneticticket()
{
int i1 = 0;
int i2 = 0;
bool exitflag = false;
bool sameflag = false;
int n = 0;
Random rand = new Random();
//---------------------------------------------------------------------------------
// generate 12 random numbers that are seleced without repetition
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < 12; i1++)
{
exitflag = false;
while (!exitflag)
{
n = rand.Next(1,25);
sameflag = false;
for (i2 = 0; i2 < i1; i2++)
{
if (ticket[i2] == n)
{
sameflag = true;
}
}
if (sameflag == false)
{
exitflag = true;
}

}
ticket[i1] = n;
}
}

//---------------------------------------------------------------------------------
// match - returns true if two tickets are matches - with a score for partials.
//---------------------------------------------------------------------------------
public bool match(int[] ticket, int i1, ref int w2)
{
bool tmp = false;
int kount = 0;
int i2 = 0;
int i3 = 0;
//---------------------------------------------------------------------------------
// check and see how many numbers match
//---------------------------------------------------------------------------------
for (i2 = 0; i2 < 12; i2++)
{
for (i3 = 0; i3 < 12; i3++)
{
if (data[i1,i3] == ticket[i2])
{
kount = kount + 1;
}
}
}
if ((kount < 5) || (kount > 7))
{
tmp = true;
}
//---------------------------------------------------------------------------------
// assign the winnings based on the rules of the Texas Lottery ALL OR NOTHING game
//---------------------------------------------------------------------------------
if ((kount == 4) || (kount == 8)) { w2 = 2; }
if ((kount == 3) || (kount == 9)) { w2 = 10; }
if ((kount == 2) || (kount == 10)) { w2 = 50; }
if ((kount == 1) || (kount == 11)) { w2 = 500; }
if ((kount == 0) || (kount == 12)) { w2 = 250000; }
return tmp;
}

//---------------------------------------------------------------------------------
// scoregenetic - to score the time series generated with forecasts.
//---------------------------------------------------------------------------------
public void scoregenetic(ref double score, ref int w)
{
int i1 = 0;
int w2 = 0;
w = 0;
//---------------------------------------------------------------------------------
// for each trial, check and see how many numbers match
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < count; i1++)
{
if (match(ticket,i1, ref w2))
{
score = score + 1;
w = w + w2;
}
}
score = score/(count + 0.00001);
if (debugflag)
{
Console1.WriteLine1("Score: "+100*score+" %");
Console1.WriteLine1("Winnings: $"+w);
}
}

//---------------------------------------------------------------------------------
// saveticket - to save the ticket to the store of winning tickets
//---------------------------------------------------------------------------------
public void saveticket()
{
int i1 = 0;
for (i1 = 0; i1 < 12; i1++)
{
winningticket[i1] = ticket[i1];
}
}

//---------------------------------------------------------------------------------
// forecast - to forecast the lottery (main routine)
//---------------------------------------------------------------------------------
public void forecast()
{
int i1 = 0;
double score = 0;
int i2 = 0;
int w = 0;
//---------------------------------------------------------------------------------
// for each trial, generate a random solution and check it's score
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < ntrials; i1++)
{
geneticticket();
if (debugflag)
{
Console1.WriteLine1("Trying Ticket: #"+(i1+1));
}
for (i2 = 0; i2 < 12; i2++)
{
if (debugflag)
{
Console1.Write1(ticket[i2]+",");
}
}
if (debugflag)
{
Console1.WriteLine1();
}
//---------------------------------------------------------------------------------
// record the trial with the highest score
//---------------------------------------------------------------------------------
scoregenetic(ref score, ref w);
if (w > winnings)
{
maxscore = score;
winnings = w;
winperticket = winnings/(count + 0.00001);
saveticket();
}

}
//---------------------------------------------------------------------------------
// add up the total winnings
//---------------------------------------------------------------------------------
twinperticket = twinperticket + winperticket;
}

//---------------------------------------------------------------------------------
// printresults - to print out the results to the screen and file
//---------------------------------------------------------------------------------
public void printresults()
{
int i1 = 0;
Console1.WriteLine1("Score:");
Console1.WriteLine1(maxscore*100+" %");
Console1.WriteLine1("Max Winnings:");
Console1.WriteLine1("$"+winnings);
Console1.WriteLine1("Winning Ticket:");
for (i1 = 0; i1 < 12; i1++)
{
Console1.Write1(winningticket[i1]+",");

}
Console1.WriteLine1();
Console1.WriteLine1("Winnings Per Ticket");
Console1.WriteLine1("$"+winperticket);
}

//---------------------------------------------------------------------------------
// tabulate - to tabulate the scoring process
//---------------------------------------------------------------------------------
public void tabulate()
{
int i1 = 0;
int i2 = 0;
int i3 = 0;
int w2 = 0;
int w = 0;
int[] ticketq = new int[12];
double score5 = 0;
int mf = 0;
//---------------------------------------------------------------------------------
// for each line, check and see how well it matched
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < count; i1++)
{
mf = 0;
w = 0;
for (i2 = 0; i2 < nt; i2++)
{
for (i3 = 0; i3 < 12; i3++)
{
ticketq[i3] = winningtickets[i2,i3];
}
if (mf >= 0)
{
if (match(ticketq,i1, ref w2))
{
score5 = score5 + 1;
w = w + w2;
mf = mf + 1;
}
}
if (i2 == (nt - 1))
{
Console1.WriteLine1("");
Console1.WriteLine1("Winnings for: "+i1+" = $"+(w-12));
}



}

}
score5 = score5/(count + 0.00001);
if (debugflag)
{
Console1.WriteLine1("Score: "+100*score5+" %");
Console1.WriteLine1("Winnings: $"+w);
}
maxscoref = score5*100;
}

//---------------------------------------------------------------------------------
// Main - main line of program aon.cs
//---------------------------------------------------------------------------------
public static void Main()
{
aon a = new aon();
int n = 0;
a.cleardata();
a.loaddata();
int i1 = 0;
//---------------------------------------------------------------------------------
// Opening display of name of the program
//---------------------------------------------------------------------------------
StreamWriter swq = new StreamWriter(a.outfile);
swq.Close();
a.twinperticket = 0;
Console1.WriteLine1();
Console1.WriteLine1(" A O N");
Console1.WriteLine1(" Genetic Lottery Forecasting System");
Console1.WriteLine1(" for the");
Console1.WriteLine1(" All or Nothing Contest");
Console1.WriteLine1(" of the");
Console1.WriteLine1(" Texas Lottery");
Console1.WriteLine1(" (c) 2020 - LG Soft");
Console1.WriteLine1();
//---------------------------------------------------------------------------------
// Loop through each of the N (50) tickets
//---------------------------------------------------------------------------------
for (n = 0; n < nt; n++)
{
Console1.WriteLine1();
Console1.WriteLine1("Ticket Generation: "+(n+1));
//---------------------------------------------------------------------------------
// initialize variables
//---------------------------------------------------------------------------------
for (i1 = 0; i1 < 12; i1++)
{
a.winningticket[i1] = 0;
}
a.maxscore = 0;
a.winnings = 0;
a.winperticket = 0;
//---------------------------------------------------------------------------------
// forecast for that ticket
//---------------------------------------------------------------------------------
a.forecast();
//---------------------------------------------------------------------------------
// store printout of results and tabulate the amount won overall
//---------------------------------------------------------------------------------
a.printresults();
for (i1 = 0; i1 < 12; i1++)
{
a.winningtickets[n,i1] = a.winningticket[i1];
}
}
a.tabulate();
//---------------------------------------------------------------------------------
// print the final win/loss ratio and final amount won overall
//---------------------------------------------------------------------------------
Console1.WriteLine1();
Console1.WriteLine1("Final Score with Combined Tickets:");
Console1.WriteLine1(a.maxscoref+" %");
Console1.WriteLine1("Final Combined Win Per Ticket:");
Console1.WriteLine1("$"+a.twinperticket);
}
}

Entry #1

Comments

This Blog entry currently has no comments.

Post a Comment

Please Log In

To use this feature you must be logged into your Lottery Post account.

Not a member yet?

If you don't yet have a Lottery Post account, it's simple and free to create one! Just tap the Register button and after a quick process you'll be part of our lottery community.

Register