int[] arr = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; // Print 5 random elements picked from arr foreach (e; sample(arr, 5)) { writeln(e); } // Print 5 random elements picked from arr, // using a specified random number generator auto gen = new Random(unpredictableSeed); foreach (e; sample(arr, 5, gen)) { writeln(e); }
The alias randomSample is available to ease migration for code written using $(PHOBOSXREF random, randomSample).
Selects a random subsample out of r, containing exactly n elements. The order of elements is the same as in the original range. The total length of r must be known. If total is passed in, the total number of elements available to sample is considered to be total. Otherwise, Sample uses r.length.
Sample implements Jeffrey Scott Vitter's Algorithm D (see Vitter 1984, 1987), which selects a sample of size n in O(n) steps and requiring O(n) random variates, regardless of the size of the data being sampled. The exception to this is if traversing k elements on the input range is itself an O(k) operation (e.g. when sampling lines from an input file), in which case the sampling calculation will inevitably be of O(total).
Sample will throw an error if total is verifiably less than the total number of elements available in the input, or if n > total.
If no random number generator is passed to sample, the thread-local default RNG rndGen will be used as the source of randomness.