hap.random

hap._random is a random number generation library for the D programming language. It is intended as a replacement for Phobos’ std._random, and addresses a number of issues encountered in that module: in particular, hap._random implements random number generators and related entities as reference types rather than value types, which avoids a number of problems that can arise with the standard library RNGs. It is however largely derivative of std._random and to the greatest extent possible implements the same API, with some functional additions, notably the random distribution ranges.

The library name, hap, is Welsh for “random” or “chance”. :-)

The functionality implemented by this package is divided into four different groups:

  • _random number generators, deterministic pseudo-random algorithms;
  • _random distributions;
  • _random adaptors, such as shuffling or sampling objects;
  • traits related to random number generation.

The hap._random package will import all of the above functionality. Alternatively, individual modules can be imported as required.

Experimental functionality, not fully developed but available as a technology preview, includes:

  • _random devices, non-deterministic sources of randomness.

This functionality will not be imported as part of the main hap._random package but can be imported via the individual modules. It should be used with some caution, as its API may change significantly in future releases.

Migration: To use hap._random instead of std._random it should suffice to:

  • replace import std._random with import hap._random
  • insert new before every instantiation of an RNG. For example, instead of auto rng = Random(unpredictableSeed) put instead, auto rng = new Random(unpredictableSeed).
  • optionally, the functions randomCover, randomSample and randomShuffle may be replaced by cover, sample and shuffle; however, this is optional, not required.

Note: Currently only one function is known to produce different behaviour to its std._random counterpart: hap._random.distribution.dice uses a different algorithm to std._random.dice.

Warning: Bear in mind that non-reference-type RNGs used in conjunction with this package will almost certainly generate erroneous results. In particular this package should not be used together with std._random itself.

Modules

adaptor
module hap.random.adaptor

Implements algorithms that transform the output of uniform random number generators into other random behaviours, such as shuffling, sampling, and so on. Typically these are implemented as range objects that wrap a provided RNG instance.

device
module hap.random.device

Provides access to non-deterministic sources of randomness. These are implemented as Input Ranges and in many cases are architecture- or OS-dependent.

distribution
module hap.random.distribution

Implements algorithms for generating random numbers drawn from different statistical distributions. Where possible, each random distribution is provided in two different forms:

generator
module hap.random.generator

Implements algorithms for uniform pseudo-random number generation. Following the definition used in the C++11 Standard Template Library $(LESS)_random$(GREATER) header, a uniform random number generator is defined to be an Input Range whose values are unsigned integer values drawn from the closed interval [min, max], such that each value among the possible set of results has (ideally) equal probability of being next in the sequence. Pseudo-random number generators are typically implemented as Forward Ranges, but this is not a requirement.

traits
module hap.random.traits

Implements compile-time checks for different features of random number generating code.

Public Imports

hap.random.adaptor
public import hap.random.adaptor;
Undocumented in source.
hap.random.distribution
public import hap.random.distribution;
Undocumented in source.
hap.random.generator
public import hap.random.generator;
Undocumented in source.
hap.random.traits
public import hap.random.traits;
Undocumented in source.

Meta

Source

See Source File
$(HAPSRC hap/_random/package.d)

Authors

Andrei Alexandrescu, Chris Cain, Andrej Mitrović, Masahiro Nakagawa, Joseph Rushton Wakeling