Mir Random and Dlang Ranges [Example]

Ilya Yaroshenko via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 25 06:27:13 PST 2016


The discussion [2] about Mir Random [1] and Range API become a 
holy war [2]. I am putting the following example here. It also 
can be found at GitHub[1].

------
import std.range, std.stdio;

import random;
import random.variable: NormalVariable;

// Engines are allocated on stack or global

auto rng = Random(unpredictableSeed);

// Engines are passed by reference to algorithms

auto sample = rng

     // Random variables are passed by value

     .randomRange(NormalVariable!double(0, 1))

      // Fix sample length equal to 1000 elements (Input Range API)

     .take(1000)

     // Allocates memory and performs computation

     .array;

writeln(sample);
------

If you still think that Dlang Random API should be a Range API 
_by default_ instead of opCall, please submit a solid Range API 
solution, that will be fine for the real world applications such 
as Normal Variance Mean Mixtures [3], be @nogc @safe and be 
without data duplication. Only predefined ranges should be used 
for NormalVariable and the second distribution. It is an API 
incapsulation problem that have a nice solution using opCall 
because RandomVariables are tuples of params with opCall(Engine) 
method.

[1] https://github.com/libmir/mir-random
[2] 
http://forum.dlang.org/thread/zlhuomzishrqrjjpxfho@forum.dlang.org
[3] https://en.wikipedia.org/wiki/Normal_variance-mean_mixture

Best regards,
Ilya


More information about the Digitalmars-d mailing list