Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count help

Status
Not open for further replies.

trevorh13

Instructor
Sep 18, 2000
132
GB
I am newish to programing and very much a C# newbie. I am trying to learn by doing as much as by reading from books and have set myself the challenge of creating a small program to select 6 numbers from 49 at random. Basically it is a lottery number selector program.

I have a liststring with values 1,2,3 etc to 49. These are then selected and configured as the text property of labels. As they are selected the Count method is used to track of the numbers selected. These numbers are then removed from the liststring.

My program works fine the first time my generate button is clicked. What I want to know is how to reset the program so that if the generate button is clicked again the full list of numbers is available within the liststring. Eg. Reset the count to null.

Any help would be appreciated.

Kind regards
 
there are numerous ways to do this. What do you currently have?


Jason Meckley
Programmer

faq855-7190
faq732-7259
 
This is what I have at present;-

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 lottery_numbers
{
public partial class Form1 : Form
{
// Use this random object to generate numbers
Random random1 = new Random();

// each number is a number that can be selected at random"
List<string> numberList = new List<string>()
{
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
"43", "44", "45", "46", "47", "48", "49"
};
/// <summary>
/// Assign a number at random to each of the four label boxes
/// </summary>
private void AssignNumbers()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label numLabel = control as Label;
if (numLabel != null)
{
int randomNumber = random1.Next(numberList.Count);
numLabel.Text = numberList[randomNumber];
numberList.RemoveAt(randomNumber);
}

}
}

public Form1()
{
InitializeComponent();
}

private void generateButton_Click(object sender, EventArgs e)
{
AssignNumbers();
}
}
}
 
there are a couple things i see.
to start you are storing strings instead of numbers. since you are working with numbers, why not store them as numbers?

second. indexes are zero base, not 1 base. so a collection of 3 values are indexed 0,1,2

third. while it's not a huge problem in this scenario, there isn't a need to load all 49 values in when the form first loads.

4th. iterating over the controls is making alot of assumuptions about the UI. it will never change, all controls are labels, etc. it would be more explicit to load the specific controls into a list and iterate over that list.

here is another approach to the scenario. note I'm using a Set<>. sets have a special contextual meaning that a value can appear exactly once in the collection.
Code:
var rand = new Random(DateTime.Now.Ticks);
var numbers = new Set<int>();

while(numbers.Count <= number of controls)
{
   var number = rand.Next(1, 49);
   numbers.Add(number);
}
foreach(var number in numbers)
{
   var control = get next control
   control.Text = number.ToString();
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top