I need to print numbers from 1 to 50 in random order without repeating it .
static void Main(string[] args)
{
ArrayList r = new ArrayList();
Random ran = new Random();
for (int i = 0; i < 50; i++)
{
r.Add(ran.Next(1,51));
}
for (int i = 0; i < 50; i++)
Console.WriteLine(r[i]);
Console.ReadKey();
}
If you don't want to repeat the numbers between 1 and 50, your best bet is to populate a list with the numbers 1 to 50 and then shuffle the contents. There's a good post on shuffling here: Randomize a List<T>
All you need to do is this check if the number already exists in the list and if so get another one:
Edit: This will greatly increase the effeciency, preventing long pauses waiting for a non-collision number:
What you want here is the Fisher Yates Shuffle
Here is the algorithm as implemented by Jeff Atwood