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;
}
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.