A Simple Six Number Lottery Number Generator in C#.net Console Application

This is just a simple c#.net console application that creates six numbers, that are in order from lowest to highest. You could try your luck and put the numbers on. This is my first programming article.

This article is simple and can be used in visual studio.net 2008. If you have a earier version off visual stuido .net you should still be able to get the same results.  

Step 1:

Create a new visual studio.net console application project.

File-New-Project

Is found at the upper left hand corner.

You will see the below image.

Step 2:

Once you have created the project and chosen where you want to save, you will see the below image. This is the default class, which visual studio creates for you.

It also creates a solution explorer, where classes , folders etc are kept.

Step 3:

The first thing I decided to do, was to find out how to create a random number.  Looking through msdn help there was a random class, which would do the job for me.

My next step was to create a method called CreateLotteryNumber, which is placed inside a class.

The signature of a method can also be found at msdn help.

Below is my method that I created to generate a random number.

I also write the result out to the console window.

 //put your methods in here to run your code

static void Main(string[] args)

{

//call the method, which will run

//the code inside it

First random number displayed below in the console window.

Step 4:

I now had to write the second version of the method called CreateLotteryNumbers to create 6 numbers and write the values out to the console.

I used a for loop to achive this.

 static void CreateLotteryNumbers()//create a instance of the random class

{

Random r = new Random();

//int variable to hold the number

int number = 0;

//a loop that loops 6 times

for (int j = 0; j < 6; j++)

{

//store the created number

number = r.Next(1, 49);

//write numbers out to the console

Console.WriteLine(number.ToString()

);

The results of the 6 numbers created in the console window below.

Step 5:

The is a problem with the above solution, you get 6 numbers, but if you kept running the program at one point you would get the same numbers.

Below is a screen shot of that.

To over come this problem I had to write another method that would check each number was unique each time one was created. 

The method would be called IsUnique, that would take 2 parameters a array of numbers and the random number to be checked.

The method will return true or false if the number is unique.

//methodpublic static bool IsUnique(int[] numbers, int number)

{

//bool variablebool numberExsists = false;

//loop through the passed in arrayfor (int i = 0; i < numbers.Length; i++)

{

//check if the is equal to any other

//number in the arrayif(numbers[i] == number)

{

//return true if the number exsists

numberExsists =

}

}

//return the result if number unique (true or false)return numberExsists;

true;

Once this method was written I had to change the CreateLotteryNumbers method to use the IsUnique method and pass in the required parameters.

The method now uses a do and while loop inside the for loop, which you can look up at msdn help for furthur information. It also uses a array to hold the numbers. The newly created random number, will be checked against the array to see if the number is not all taken using the IsUnique method.

Below is the updated method.

 //method

static void CreateLotteryNumbers()

{

//a int array to hold 6 numbers

int[]numbers = new int[6];

//create a instance of the random class

Random r = new Random();

//int variable to hold the number

int number = 0;

//a loop that loops 6 times

for (int j = 0; j < 6; j++)

{

//a do statement

do

{

//store the created number

number = r.Next(1, 49);

}

//pass the array and created number into the IsUnique method

//the while statement will keep createing numbers untill one unique

while(IsUnique(numbers, number));

//fill the array with numbers using j as a counter to repersent

//the index of the array so we can set

//that position

numbers[j] = number;

//write numbers out to the console

Console.WriteLine(number.ToString()

);

Step 6:

The above code will create 6 unique numbers and write them out to the console, but not in order. There is a  static class called (Array) that you can use to sort arrays.  This has a method called sort on it that will sort the elements for you. This is added outside the for loop, so we know all 6 numbers have been created. I also added a method to deal with the writing to the console after the sorting has been done.

Below is the code for the finished project with updated CreateLotteryNumbers method and a ShowValues method, which takes a array as a parameter.

System;

using

System.Collections.Generic;

using

System.Text;

namespace

Lottery_Number_Generator

{

class Program

{

//put your methods in here to run your code

static void Main(string[] args)

{

//call the method, which will run

//the code inside it

CreateLotteryNumbers();

}

//method

static void CreateLotteryNumbers()

{

//a int array to hold 6 numbers

int[]numbers = new int[6];

//create a instance of the random class

Random r = new Random();

//int variable to hold the number

int number = 0;

//a loop that loops 6 times

for (int j = 0; j < 6; j++)

{

//a do statement

do

{

//store the created number

number = r.Next(1, 49);

}

//pass the array and created number into the IsUnique method

//the while statement will keep createing numbers untill one unique

while (IsUnique(numbers, number));

//fill the array with numbers using j as a counter to repersent

//the index of the array so we can set

//that position

numbers[j] = number;

}

//sort the array

Array.Sort(numbers);

//show the values

ShowValues(numbers);

}

//method

public static bool IsUnique(int[] numbers, int number)

{

//bool variable

bool numberExsists = false

;

//loop thorugh the passed in array

for (int i = 0; i < numbers.Length; i++)

{

//check if the is equal to any other

//number in the array

if (numbers[i] == number)

{

//return true if the number exsists

numberExsists =

true;

//return the result if number unique (true or false)

return numberExsists;

}

//method

public static void ShowValues(int[] numbers)

{

//loop through the array

for (int i = 0; i < numbers.Length; i++)

{

//write the results to the console

Console.WriteLine(numbers[i]);

 }

 }

I hope this article is of some help to people learning visual studio.net. This is my first article on programming, so I hope I have some good feedback, so I can write some more like this.

kered

Leave Your Response