Generate numbers and choose in memory

I want to do the following dynamically

Generate numbers from 1 to 100, and then select 25 random numbers from it and display them on the console. Any easy way to do this?

+5
source share
1 answer
IEnumerable<int> numbers = Enumerable.Range(1, 100);
Random random = new Random();

IEnumerable<int> randomSelection = numbers.OrderBy(n => random.Next()).Take(25);

foreach (int i in randomSelection)
    Console.WriteLine(i);
+2
source

All Articles