A Lottery Program in C#

So why a lottery program?? Like everyone else, I  am learning C# in any spare time I have and instead of diving in and writing a monster program I thought a nice introduction would be good. I chose the Lottery program as it allowed me to play with the .Net Random and Stack class.

The problem is this. To generate a selection of unique random values. So if I want 6 numbers and want to in effect simulate rolling a 6 sided dice I would enter 6 at the first prompt and 6 at the second. Note that because we do not want duplicate numbers your Number Range ie the second question should be either the same or larger than your first entry.

Here in the UK our national lottery requires you select 6 unique numbers whos value must be between 1 and 44 (I think) so of course you enter 6 at first prompt and 44 at second.

The random class is easy to use and after declaring a new instance using Random r = new Random(); you can use the .Next method to get a random number. The gotcha is that if you wanted a random number between 1 and 6 and did r.Next(), you would actually get a value from 0 to 5. There is another way where you can specify a from to range ie r.Next(1,6) however at this point I thought lets write a dice class so I can learn about creating my own additional classes. This class has only one method RollDice and if you pass in a value of 6 it will return from 1 to 6 and ignore zero values.

Our UK lottery requires 6 unique values so if I roll my dice how to do it. In the old days we would maybe use an array and check the array to see if my value was already there but hey! were using .NET now, there must be a better way.

The .Net framework provides a Stack class which is sort of like assembly language in that you can push items onto the stack and retrieve them later. In my case I dont care that the first item in is the last item out. However of real use is the Contains method of the Stack class which allows you to see if a value is already on the stack. Looking at the documentation it almost looks like anything can be pushed and pulled on the stack. If you want to retrieve items of the stack in the order you pushed them ie first in first out, Microsoft has also added a class called Queue which more or less works the same way as the Stack class. Instead of Push and Pull methods you have Enqueue and Dequeue (hey I didnt make these names up!).

Compile from the command prompt using csc lottery.cs

Conclusions

Microsoft have created an easy to use development framework which actually makes coding fun again! I highly recommend you read the docs thoroughly as there of many useful classes to be found. Also the development system and in particualar its context sensitive help is a joy to use and makes all the features of the .Net framework very discoverable.

Roll on Beta 2! 

Source Code

//Example Lottery program to generate a set of unique pseudo random numbers
//Written in C# with Microsoft .NET beta 2
//Shows use of Random and Stack class.
//J O'Donnell 29/06/01
//[email protected] //only change needed from beta 1 to beta 2 was to use Convert class.
namespace Lottery
{
using System;
using System.Collections;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Lottery
{
public static int Numbers, EndRange;
//Displays titles and gets start values
public static void DisplayTitles()
{
EndRange=0;
Numbers=1;
while (EndRange<Numbers)
{
System.Console.WriteLine("=============================");
System.Console.WriteLine("Lottery number generator");
System.Console.WriteLine("=============================");
System.Console.Write("How many unique numbers? : ");
Numbers=Convert.ToInt32(System.Console.ReadLine());
System.Console.Write("Enter end range : ");
EndRange =Convert.ToInt32(System.Console.ReadLine());
}
}
//Generate our unique numbers and push them onto stack
public static void GenerateNumbers(Stack s, Dice d)
{
bool exists=true;
int i;
int val;
for (i=1;i<=Numbers;i++)
{
val=d.RollDice(EndRange); //Roll EM!
exists=s.Contains(val);
while (exists==true) //Reroll if we already
{
//have that number
val=d.RollDice(EndRange);
exists=s.Contains(val);
}
s.Push(val);
}
}
//Retrieve all values from the stack and output to console
public static void DisplayResults(Stack s)
{
for (int i=1;i<=Numbers;i++)
{
System.Console.WriteLine(s.Pop());
}
}
//Entry point for program
public static int Main (string[] args)
{
Stack s = new Stack();
Dice d = new Dice();
DisplayTitles();
GenerateNumbers(s,d);
DisplayResults(s);
return 0;
}
}
//Class to simulate a dice
public class Dice
{
public int RollDice(int sides)
{
Random r = new Random();
int i=0;
while(i==0)
{
i=r.Next(sides+1);//Get another random number but
} //discard zero values
return i;
}
}
}

 


Similar Articles