randomSample

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 18 00:22:15 PDT 2014


Meta:

> You need to use the function array from std.array.
>
> import std.array;
>
> int[] source = [ ... ];
> int[] sample = randomSample(source, 3).array();

In some cases it's also useful to use std.algorithm.copy:


void main() {
     import std.stdio, std.algorithm, std.random, std.array,
            std.range;

     immutable int[9] source = iota(10, 100, 10).array;
     source.writeln;
     int[3] sample;
     source[].randomSample(sample.length).copy(sample[]);
     sample.writeln;
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list