Possible Duplicate:
Random number generator in C# - unique values
I'm trying to write a C# program that will generate a random number, check if this number is in my array, if it is, repeat generating the number, else insert this number into slot [i]
of my array.
Here is my code so far:
int check = 0;
Random rand = new Random();
int[] lotto = new int[6];
for (int i = 0; i < lotto.Length; )
{
check = rand.Next(1, 41);
while (!(lotto.Contains(check)))
{
lotto[i] = check;
i++;
}
Console.WriteLine("slot " + i + " contains " + check);
}
Console.Read();
}
UPDATE: Thanks figured it out, replaced the if with while :)