Wednesday, April 8, 2009

Somehow this just seemed relevant! ;-)

Monday, February 16, 2009

Random Quick Pick tinkerings

While visiting a local newsagent recently, I noticed that the Australian national lottery has a system that generates numbers randomly. For some reason I thought this was kind of interesting as a concept and wondered how it would be done.

The result of playing around with some C# code for a little while is as follows. Feel free to do whatever you like with it and if it wins you a bucket full of cash, let me know and have fun coding!


using System;
using System.Collections.Generic;
using System.Text;

namespace QuickPluck
{
class Program
{
static void Main(string[] args)
{
DisplayBanner();
int required = RequestedGames();
int gameNumMax = GameNumbers();
int numSeries = SeriesSequence();

for (int z = 0; z < style="color: rgb(51, 204, 0);">//time needs to be above 1000 cycles or dupes occur!
int c = 10000;
int[] numb = new int[c];
DateTime dt = new DateTime();
dt = DateTime.Now;
Random rnd = new Random(dt.Millisecond);
for (int i = 0; i < style="color: rgb(51, 204, 0);">//the magic touch!
numb[i] = rnd.Next(1, gameNumMax);
}
Array.Sort(numb);
int k = 0;
int j = 0;
int[] length = new int[50];
int[] result = new int[50];
for (int i = 0; i < style="color: rgb(51, 102, 255);">if (numb[i] == numb[i + 1])
{
result[k] = numb[i];
length[k] = j += 1;
}
else
{
k += 1;
j = 0;
}
}
Array.Sort(length, result);
switch (numSeries)
{
case 1:
Console.WriteLine(result[49]);
break;
case 2:
Console.WriteLine(result[49] + " " + result[48]);
break;
case 3:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47]);
break;
case 4:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46]);
break;
case 5:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46] + " " + result[45]);
break;
case 6:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46] + " " + result[45] + " " + result[44]);
break;
case 7:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46] + " " + result[45] + " " + result[44]+ " " + result[43]);
break;
case 8:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46] + " " + result[45] + " " + result[44]+ " " + result[43]+ " " + result[42]);
break;
case 9:
Console.WriteLine(result[49] + " " + result[48] + " " + result[47] + " " + result[46] + " " + result[45] + " " + result[44] + " " + result[43] + " " + result[42] + " " + result[41]);
break;
default:
Console.WriteLine("Invalid selection. Please select numbers from 1 to 9!");
break;
}
}
ExitNow();
}
private static void DisplayBanner()
{
Console.WriteLine("Welcome to QuickPluck personal lottery number generator.");
Console.WriteLine("\n");
Console.WriteLine("The idea is simple.");
Console.WriteLine("1 - Enter the number of games to play as required.");
Console.WriteLine("2 - Enter the maximum numbers allowed in the game.");
Console.WriteLine("3 - Enter the numbers per line in each game (1-9) - presto!");
Console.WriteLine("\n");
}
private static int GameNumbers()
{
int myGames = 0;
Console.WriteLine("How many possible numbers? eg; 45 number game");
try
{
myGames = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException)
{
UserFud();
myGames = 0;
}
int gameNumMax = myGames;//required number system. ie; 46 nums
return gameNumMax;
}
private static int RequestedGames()
{
int myValue = 0;
Console.WriteLine("How many games required? eg; 1 to 40");
try
{
myValue = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException)
{
UserFud();
myValue = 0;
}
int required = myValue;//number of results required?
return required;
}
private static int SeriesSequence()
{
int numSeries = 0;
Console.WriteLine("How many numbers per game? eg; (5) numbers, (6) numbers or super 7's? (7)");
try
{
string seriesNum = Console.ReadLine();
numSeries = int.Parse(seriesNum);
}
catch (FormatException)
{
UserFud();
numSeries = 0;
}
return numSeries;
}
private static void UserFud()
{
Console.WriteLine("\n");
Console.WriteLine("must be numeric!");
Console.WriteLine("\n");
}
private static void ExitNow()
{
Console.WriteLine("\n");
Console.WriteLine("Thanks for Plucking! Any key exits.");
Console.WriteLine("\n");
string exiting = Console.ReadKey().ToString();
if (exiting.Length > 0)
{
Environment.Exit(0);
}
}
}
}