The OpenD Programming Language

PiecewiseLinearVariable

Piecewise constant variable.

Constructors

this
this(T[] points, T[] weights, T[] areas)

Members

Functions

max
T max()
min
T min()
opCall
T opCall(RNG gen)
T opCall(RNG* gen)

Complexity: O(log n) where n is the number of weights.

Manifest constants

isRandomVariable
enum isRandomVariable;

Examples

import mir.random.engine;
auto gen = Random(unpredictableSeed);
// increase the probability from 0 to 5
// remain flat from 5 to 10
// decrease from 10 to 15 at the same rate
double[] i = [0, 5, 10, 15];
double[] w = [0, 1,  1,  0];
auto pcv = piecewiseLinearVar(i, w, new double[w.length - 1]);
static assert(isRandomVariable!(typeof(pcv)));

int[int] hist;
foreach(_; 0 .. 10000)
    ++hist[cast(int)pcv(gen)];

//import std.stdio;
//import mir.ndslice.topology: repeat;
//foreach(j; 0..cast(int)i[$-1]+1)
//    if(auto count = j in hist)
//        writefln("%2s %s", j, '*'.repeat(*count / 100));

//////// output example /////////
/+
 0 *
 1 **
 2 *****
 3 *******
 4 ********
 5 **********
 6 *********
 7 *********
 8 **********
 9 *********
10 *********
11 *******
12 ****
13 **
14 *
+/
import mir.random.engine;
Random* gen = threadLocalPtr!Random;
// increase the probability from 0 to 5
// remain flat from 5 to 10
// decrease from 10 to 15 at the same rate
double[] i = [0, 5, 10, 15];
double[] w = [0, 1,  1,  0];
auto pcv = PiecewiseLinearVariable!double(i, w, new double[w.length - 1]);

int[int] hist;
foreach(_; 0 .. 10)
    ++hist[cast(int)pcv(gen)];

Meta