Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,824 answers

573 users

How to select N reservoir items randomly from a list in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
	public static void Main(string[] args)
	{
		var list = new List<int> {4, 9, 14, 96, 13, 0, 3, 99, 19, 2, 80, 1, 7};
		int N = 5;
		
		Random rand = new Random(); 
 
		var reservoir = list.OrderBy(x => rand.Next()).Take(N);

		Console.WriteLine(string.Join(' ', reservoir));
	}
}




/*
run:
  
1 7 80 14 19
  
*/

 





answered Feb 3 by avibootz
...