The OpenD Programming Language

sample

Selects a random subsample out of range, containing exactly n elements. The order of elements is the same as in the original range.

  1. auto sample(G gen, Range range, size_t n)
    static if(is(typeof( )))
    sample
    (
    G
    Range
    )
    (
    G gen
    ,
    Range range
    ,
    size_t n
    )
    if (
    isInputRange!Range &&
    hasLength!Range
    &&
    (
    __traits(hasMember, Range, "popFrontExactly") ||
    )
    &&
    &&
    (
    is(G == class) ||
    is(G == interface)
    )
    )
  2. auto sample(G* gen, Range range, size_t n)
  3. auto sample(G gen, Range range, size_t n)
  4. auto sample(Range range, size_t n)

Parameters

range Range

range to sample from

gen G

random number engine to use

n size_t

number of elements to include in the sample; must be less than or equal to the range.length Complexity: O(n)

Return Value

Type: auto

RandomSample over the range.

Examples

Default RNE

// mir.ndslice package is required for 'iota', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    import mir.ndslice.topology: iota;

    auto sample = 100.iota.sample(7);
    assert(sample.length == 7);
}
// mir.ndslice package is required for 'iota', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    import mir.algorithm.iteration: equal;
    import mir.ndslice.topology: iota;
    import mir.random.engine.xorshift;

    // Using pointer to RNE:
    setThreadLocalSeed!Xorshift(112); //Use a known seed instead of a random seed.
    Xorshift* gen_ptr = threadLocalPtr!Xorshift;
    auto sample1 = gen_ptr.sample(100.iota, 7);

    // Using alias of local RNE:
    Xorshift gen = Xorshift(112);
    auto sample2 = 100.iota.sample!gen(7);

    assert(sample1.equal(sample2));
}

Meta