Lotto Number Generator Form Class (C# .NET)

March 26, 2008 at 12:17 pm Leave a comment

This code demonstrates another way to generate unique random numbers, using an if statement in nested for loops to check and reassign numbers where it is found they have already been selected.

Not to the most graceful way of going about this, but works none the less. There is another random number generator class in this blog which is a bit neater.

Download Code File: LottoForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lotto
{
public partial class LottoForm : Form
{
public LottoForm()
{
InitializeComponent();
}

Random m_random = new Random();

int[] selectedNumbers = new int[7];

private void goButton_Click(object sender, EventArgs e)
{
// For each array member, assign a random number
for (int numberCount = 0; numberCount < selectedNumbers.Length; numberCount++)
{

int newNumber = m_random.Next(1, 50);

for (int i = 0; i < numberCount; i++)
{
if (newNumber == selectedNumbers[i])
{
// MessageBox.Show(“A duplicate (” + newNumber + “) has occured”);
newNumber = m_random.Next(1, 50); // Assign a new number
// MessageBox.Show(“Replacing with ” + newNumber);
continue; // Test again
}
}

selectedNumbers[numberCount] = newNumber;

}

// Place the bonus number in the 7th space before sorting

textBox7.Text = selectedNumbers[6].ToString();

// Create and populate intemediary array which we can exclude the bonus number from
int[] nonBonusNumbersArray = new int[6];

for (int x = 0; x < nonBonusNumbersArray.Length; x++)
{
nonBonusNumbersArray[x] = selectedNumbers[x];
}

// Sort the intemediary array
Array.Sort(nonBonusNumbersArray);

// Place sorted numbers in non bonus text boxes
textBox1.Text = nonBonusNumbersArray[0].ToString();
textBox2.Text = nonBonusNumbersArray[1].ToString();
textBox3.Text = nonBonusNumbersArray[2].ToString();
textBox4.Text = nonBonusNumbersArray[3].ToString();
textBox5.Text = nonBonusNumbersArray[4].ToString();
textBox6.Text = nonBonusNumbersArray[5].ToString();

label9.Text = “”;
label9.Text += “Your new numbers are: \n\n”;
label9.Text += nonBonusNumbersArray[0].ToString();
label9.Text += “, “;
label9.Text += nonBonusNumbersArray[1].ToString();
label9.Text += “, “;
label9.Text += nonBonusNumbersArray[2].ToString();
label9.Text += “, “;
label9.Text += nonBonusNumbersArray[3].ToString();
label9.Text += “, “;
label9.Text += nonBonusNumbersArray[4].ToString();
label9.Text += “, “;
label9.Text += nonBonusNumbersArray[5].ToString();
label9.Text += ” (Bonus: “;
label9.Text += selectedNumbers[6].ToString();
label9.Text += “)”;

}

}
}

Advertisements

Entry filed under: ASP.NET, C#. Tags: , , , , , .

Unique Random Number Generator Class (C# .NET) Search Engine Optimization Easy Guide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

March 2008
M T W T F S S
    May »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Most Recent Posts


%d bloggers like this: