import java.util.*; class SOfN { private static final Random rand = new Random(); private List sample; private int i = 0; private int n; public SOfN(int _n) { n = _n; sample = new ArrayList(n); } public List process(T item) { if (++i <= n) { sample.add(item); } else if (rand.nextInt(i) < n) { sample.set(rand.nextInt(n), item); } return sample; } } public class AlgorithmS { public static void main(String[] args) { int[] bin = new int[10]; for (int trial = 0; trial < 100000; trial++) { SOfN s_of_n = new SOfN(3); for (int i = 0; i < 9; i++) s_of_n.process(i); for (int s : s_of_n.process(9)) bin[s]++; } System.out.println(Arrays.toString(bin)); } }