The OpenD Programming Language

randomSlice

Allocates ndslice (vector, matrix, or tensor) and fills it with random numbers. If no variable is specified each element e is generated per rand!(typeof(e)).

Parameters

gen

random engine (optional, param or template param)

lengths size_t[N]

one or more lengths

Examples

Random sample from Normal distribution

// mir.ndslice package is required for 'randomSlice', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    import mir.random.variable: normalVar;
    // Using default RNE:
    auto sample = normalVar.randomSlice(10);
    assert(sample.shape == [10]);

    import mir.ndslice.slice: Slice;
    assert(is(typeof(sample) == Slice!(double*)));

    // Using pointer to RNE:
    sample = threadLocalPtr!Random.randomSlice(normalVar, 15);

    // Using local RNE:
    auto rng = Random(12345);
    sample = rng.randomSlice(normalVar, 15);
}

Random sample from uniform distribution strictly in the interval (-1, 1).

// mir.ndslice package is required for 'randomSlice', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    import mir.algorithm.iteration: all;
    import mir.math.common: fabs;
    // Using default RNE:
    auto sample = randomSlice!double(10);
    assert(sample.shape == [10]);

    import mir.ndslice.slice: Slice;
    assert(is(typeof(sample) == Slice!(double*)));
    assert(sample.all!(a => a.fabs < 1));

    // Using pointer to RNE:
    sample = threadLocalPtr!Random.randomSlice!double(15);

    // Using local RNE:
    auto rng = Random(12345);
    sample = rng.randomSlice!double(15);

    // For complex numbers:
    auto csample = randomSlice!cdouble(10);
}

Random sample from 3D-sphere distribution

// mir.ndslice package is required for 'randomSlice', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    import mir.random.ndvariable: sphereVar;
    // Using default RNE:
    auto sample = sphereVar.randomSlice(10, 3);
    assert(sample.shape == [10, 3]);
    // 10 observations from R_3

    import mir.ndslice.slice: Slice;
    assert(is(typeof(sample) == Slice!(double*, 2)));

    // Using pointer to RNE:
    sample = threadLocalPtr!Random.randomSlice(sphereVar, 15, 3);

    // Using local RNE:
    auto rng = Random(12345);
    sample = rng.randomSlice(sphereVar, 15, 3);
}

Random binary data

// mir.ndslice package is required for 'randomSlice', it can be found in 'mir-algorithm'
static if (is(typeof({ import mir.ndslice.slice; })))
{
    // Using default RNE:
    auto sample = randomSlice!ulong(15);
    assert(sample.shape == [15]);

    import mir.ndslice.slice: Slice;
    assert(is(typeof(sample) == Slice!(ulong*)));

    // Using pointer to RNE:
    sample = randomSlice!ulong(threadLocalPtr!Random, 15);

    // Using local RNE:
    auto rng = Random(12345);
    sample = randomSlice!ulong(rng, 15);
}

Meta