I am building a c# windows application form. I have so far produced a random number algorithm which populates a list box with 30 (int) random numbers. I am not using an array as the list box is an array itself.
My problem is when I click on a button to generate 30 random numbers. the numbers are populated but I am then at risk of producing duplicate numbers which I want to prevent from happening.
I would like to be able to use a HashSet or Distinct method to prevent duplication. I have used a simple .Distinct to remove duplicates but it doesn't prevent duplicates from being populated to the list.
note: I can't sort the list box as I have separate functionality for that.
private void GenerateNumbers_Click(object sender, EventArgs e)
{
List<int> nums = new List<int>();
Random rnd = new Random();
//HashSet<int> duplicate = new HashSet<int>();
//IEnumerable<int> RemoveDuplicates = numberList.Distinct();
int produce;
for (int i = 0; i < 30; i++)
{
produce = rnd.Next(1, 250);
this.nums.Items.Insert(0, produce.ToString());
}
GenerateNumbers.Enabled = false;
}