Dlang Random Number Generators

Package Information

Version0.1.2 (2016-Dec-15)
CopyrightCopyright © 2016, Ilya Yaroshenko
AuthorsIlya Yaroshenko, Andrei Alexandrescu, Masahiro Nakagawa
Registered byIlya Yaroshenko



To use this package, put the following dependency into your project's dependencies section:



Gitter Bountysource

Circle CI Build Status Build status

Dub version Dub downloads License


Professional Random Number Generators


#!/usr/bin/env dub
/+ dub.json:
    "name": "test_random",
    "dependencies": {"mir": "0.22.0"}

import std.range, std.stdio;

import mir.random;
import mir.random.variable: NormalVariable;
import mir.random.algorithm: range;

void main(){
    auto rng = Random(unpredictableSeed);        // Engines are allocated on stack or global
    auto sample = rng                            // Engines are passed by reference to algorithms
        .range(NormalVariable!double(0, 1))      // Random variables are passed by value
        .take(1000)                              // Fix sample length to 1000 elements (Input Range API)
        .array;                                  // Allocates memory and performs computation


Comparison with Phobos

  • Does not depend on DRuntime (Better C concept)

random (new implementation and API)
  • Mir Random rand!float/rand!double/rand!real generates saturated real random numbers in (-1, 1). For example, rand!real can produce more then 2^78 unique numbers. In other hand, std.random.uniform01!real produces less then 2^31 unique numbers with default Engine.
  • Mir Random fixes Phobos integer underflow bugs.
  • Addition optization was added for enumerated types.
ramdom.variable (new)
  • Uniform
  • Exponential
  • Gamma
  • Normal
  • Cauchy
  • ...
random.algorithm (new)
  • Range API adaptors

random.engine.* (fixed, reworked)
  • opCall API instead of range interface is used (similar to C++)
  • No default and copy constructors are allowed for generators.
  • @RandomEngine UDA is used for for Engines instead of a enum flag.
  • unpredictableSeed has not state, returns size_t
  • Any unsigned generators are allowed.
  • min proporty was removed. Any integer generator can normalize its minimum down to zero.
  • Mt19937: +100% performance for initialization.
  • Mt19937: +54% performance for generation.
  • Mt19937: fixed to be more CPU cache friendly.
  • 64-bit Mt19937 initialization is fixed
  • 64-bit Mt19937 is default for 64-bit targets
  • [WIP] additional Engines, see

Available versions

0.1.2 0.1.1 0.1.0 0.0.1 0.0.1-beta5 0.0.1-beta4 0.0.1-beta3 0.0.1-beta2 0.0.1-beta1 0.0.1-beta0 ~master