I want to generate a list of N different random numbers:
public static List<int> GetRandomNumbers(int count)
{
List<int> randomNumbers = new List<int>();
for (int i=0; i<count; i++)
{
int number;
do number = random.Next();
while (randomNumbers.Contains(number));
randomNumbers.Add(number);
}
return randomNumbers;
}
But I feel there is a better way. This do...while
loop especially makes this ugly. Any suggestions on how to improve this?
Use comments to ask for more information or clarify a question or answer.
You can always comment on your own questions and answers. Once you earn 50 reputation, you can comment on anybody's post.
Remember: we're all here to learn, so be friendly and helpful!