Reputation: 19
I'm trying to create a random number generator between 1 and 500 that does not repeat itself. I made an array to keep track of if a generated number has been used but it doesn't seem to be working
if (drawnTF[num] == false)
{
labels[labelNum].Visible = false;
txtLatestDrawnNum.Text = "Latest Drawn Number: " + latestDrawnNum.ToString("000");
drawnTF[num] = true;
}
else
{
while (drawnTF[num] == true)
{
num = random.Next(0, 499);
if (drawnTF[num] == false)
{
labelNum = 499 - num;
labels[labelNum].Visible = false;
txtLatestDrawnNum.Text = "Latest Drawn Number: " + latestDrawnNum.ToString("000");
drawnTF[num] = true;
}
}
}
Upvotes: 1
Views: 410
Reputation: 130
You can try to put 500 numbers in an list and do a next(0,values.Length). Each time you pick a random number you remove it from that list. This way you will always have different numbers.
Upvotes: 5