Jasl
Jasl

Reputation: 1405

Generate Numbers and Select 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 it in a console. Any easy way to do so?

Upvotes: 5

Views: 80

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126992

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);

Upvotes: 2

Related Questions