Developer2U
Programmer
I have created a random number generator. What I am trying to accomplish is to add a specific number of random numbers (from a sorted list of numbers 1-187) to a list box. I want to then sort the numbers in numerical order in the list box. Once a number is selected I don't want to pick that number again.
Steps
1. On form load, numbers 1-187 are added to a sorted list (stlNumbers).
2. User selects number of random numbers to generate from spinner control
3. User clicks button control
4. Call the GetRandom method to generate a list of random numbers. Add each random number to an array list
5. Sort the array list (ShowList)
6. Iterate through the array list and add items to the list box (ShowList)
The user selects the number of random numbers to choose from. I'm having trouble populating the list box with random numbers. If I don't include a message box as I am selecting a new random number then the list is populated with consecutive numbers. If I include a message box, it works fine, adding random numbers.
Here is the code I've used:
Steps
1. On form load, numbers 1-187 are added to a sorted list (stlNumbers).
2. User selects number of random numbers to generate from spinner control
3. User clicks button control
4. Call the GetRandom method to generate a list of random numbers. Add each random number to an array list
5. Sort the array list (ShowList)
6. Iterate through the array list and add items to the list box (ShowList)
The user selects the number of random numbers to choose from. I'm having trouble populating the list box with random numbers. If I don't include a message box as I am selecting a new random number then the list is populated with consecutive numbers. If I include a message box, it works fine, adding random numbers.
Here is the code I've used:
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Languages
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmLanguageSelection : System.Windows.Forms.Form
{
private SortedList stlNumbers = new SortedList();
//private SortedList selectedLanguages = new SortedList();
private ArrayList arlNumbers = new ArrayList();
private void btnSelection_Click(object sender, System.EventArgs e)
{
//Call the GetRandom method
int zz = Convert.ToInt32(nudNumberQuestions.Value);
GetRandom(zz);
ShowList();
}
private void ShowList()
{
//Sort the array list of random numbers
arlNumbers.Sort();
//iterate through the array list of random numbers and
//add them to a list box
foreach(int ar in this.arlNumbers)
{
this.lstRandom.Items.Add(ar.ToString());
}
this.lstRandom.Visible = true;
this.arlNumbers = null;
this.stlNumbers = null;
}
private void GetRandom(int intCount)
{
for (int x = 1; x < intCount +1; x++)
{
//Instantiate a new random number
Random r = new Random();
try
{
//Declare an integer to hold the random number
int i = r.Next(stlNumbers.Count);
//Declare a string to hold the string value of
//the value in the sorted list corresponding
//to the random value selected
MessageBox.Show(stlNumbers.GetKey(i).ToString());
arlNumbers.Add(stlNumbers.GetKey(i));
/*Remove the item from the sorted list so that it
can't be selected again*/
stlNumbers.RemoveAt(i);
}
catch(Exception err)
{
MessageBox.Show(err.ToString());
}
finally
{
//clean up code
r = null;
}
}
}
private void frmLanguageSelection_Load(object sender, System.EventArgs e)
{
/*add the key and value pairs of the languages sorted list*/
for(int i = 1;i<187;i++)
{
stlNumbers.Add(i,i);
}
}
}//end of class
}//end of namespace