Module std.random
Facilities for random number generation.
| Category | Functions | 
|---|---|
| Uniform sampling | uniformuniform01uniformDistribution | 
| Element sampling | choicedice | 
| Range sampling | randomCoverrandomSample | 
| Default Random Engines | rndGenRandomunpredictableSeed | 
| Linear Congruential Engines | MinstdRandMinstdRand0LinearCongruentialEngine | 
| Mersenne Twister Engines | Mt19937Mt19937_64MersenneTwisterEngine | 
| Xorshift Engines | XorshiftXorshiftEngineXorshift32Xorshift64Xorshift96Xorshift128Xorshift160Xorshift192 | 
| Shuffle | partialShufflerandomShuffle | 
| Traits | isSeedableisUniformRNG | 
Disclaimer: The random number generators and API provided in this module are not designed to be cryptographically secure, and are therefore unsuitable for cryptographic or security-related purposes such as generating authentication tokens or network sequence numbers. For such needs, please use a reputable cryptographic library instead.
The new-style generator objects hold their own state so they are
immune of threading issues. The generators feature a number of
well-known and well-documented methods of generating random
numbers. An overall fast and reliable means to generate random numbers
is the Mt19937 generator, which derives its name from
"Mersenne Twister
with a period of 2 to the power of
19937". In memory-constrained situations,
linear congruential generators such as MinstdRand0 and MinstdRand might be
useful. The standard library provides an alias Random for
whichever generator it considers the most fit for the target
environment.
In addition to random number generators, this module features distributions, which skew a generator's output statistical distribution in various ways. So far the uniform distribution for integers and real numbers have been implemented.
Credits
The entire random number library architecture is derived from the excellent C++0X random number facility proposed by Jens Maurer and contributed to by researchers at the Fermi laboratory (excluding Xorshift).
Example
import stdFunctions
| Name | Description | 
|---|---|
| 
									choice(range, urng)
								 | Returns a random, uniformly chosen, element efrom the suppliedRange range. If no random number generator is passed, the defaultrndGenis used. | 
| 
									dice()
								 | Get a random index into a list of weights corresponding to each index | 
| 
									partialShuffle(r, n, gen)
								 | Partially shuffles the elements of rsuch that upon returningr[0 .. n]is a random subset ofrand is randomly ordered.r[n .. rwill contain the elements not inr[0 .. n].  These will be in an undefined
order, but will not be random in the sense that their order afterpartialShufflereturns will not be independent of their order beforepartialShufflewas called. | 
| 
									randomCover(r, rng)
								 | Covers a given range rin a random manner, i.e. goes through each
element ofronce and only once, just in a random order.rmust be a random-access range with length. | 
| 
									randomSample(r, n, total)
								 | Selects a random subsample out of r, containing exactlynelements. The order of elements is the same as in the original
range. The total length ofrmust be known. Iftotalis
passed in, the total number of sample is considered to betotal. Otherwise,RandomSampleusesr. | 
| 
									randomShuffle(r, gen)
								 | Shuffles elements of rusinggenas a shuffler.rmust be
a random-access range with length.  If no RNG is specified,rndGenwill be used. | 
| 
									rndGen()
								 | Global random number generator used by various functions in this module whenever no generator is specified. It is allocated per-thread and initialized to an unpredictable value for each thread. | 
| 
									uniform(a, b)
								 | Generates a number between aandb. Theboundariesparameter controls the shape of the interval (open vs. closed on
either side). Valid values forboundariesare"[]","(]","[)", and"()". The default interval
is closed to the left and open to the right. The version that does not
takeurnguses the default generatorrndGen. | 
| 
									uniform(urng)
								 | Generates a uniformly-distributed number in the range [Tfor any integral or character typeT. If no random
number generator is passed, uses the defaultrndGen. | 
| 
									uniform01()
								 | Generates a uniformly-distributed floating point number of type Tin the range [0, 1).  If no random number generator is
 specified, the default RNGrndGenwill be used as the source
 of randomness. | 
| 
									uniformDistribution(n, useThis)
								 | Generates a uniform probability distribution of size n, i.e., an
array of sizenof positive numbers of typeFthat sum to1. IfuseThisis provided, it is used as storage. | 
| 
									unpredictableSeed()
								 | A "good" seed for initializing random number engines. Initializing with unpredictableSeed makes engines generate different random number sequences every run. | 
Structs
| Name | Description | 
|---|---|
| 
									LinearCongruentialEngine
								 | Linear Congruential generator. When m = 0, no modulus is used. | 
| 
									MersenneTwisterEngine
								 | The Mersenne Twister generator. | 
| 
									RandomCover
								 | Covers a given range rin a random manner, i.e. goes through each
element ofronce and only once, just in a random order.rmust be a random-access range with length. | 
| 
									RandomSample
								 | Selects a random subsample out of r, containing exactlynelements. The order of elements is the same as in the original
range. The total length ofrmust be known. Iftotalis
passed in, the total number of sample is considered to betotal. Otherwise,RandomSampleusesr. | 
| 
									XorshiftEngine
								 | Xorshift generator. Implemented according to Xorshift RNGs (Marsaglia, 2003) when the size is small. For larger sizes the generator uses Sebastino Vigna's optimization of using an index to avoid needing to rotate the internal array. | 
Manifest constants
| Name | Type | Description | 
|---|---|---|
| isSeedable | Test if Rng is seedable. The overload taking a SeedType also makes sure that the Rng can be seeded with SeedType. | |
| isUniformRNG | Test if Rng is a random-number generator. The overload taking a ElementType also makes sure that the Rng generates values of that type. | 
Aliases
| Name | Type | Description | 
|---|---|---|
| MinstdRand | LinearCongruentialEngine!(uint,48271,0,2147483647) | Define LinearCongruentialEngine generators with well-chosen
parameters. MinstdRand0implements Park and Miller's "minimal
standard" generator that uses 16807 for the multiplier.MinstdRandimplements a variant that has slightly better spectral behavior by
using the multiplier 48271. Both generators are rather simplistic. | 
| MinstdRand0 | LinearCongruentialEngine!(uint,16807,0,2147483647) | Define LinearCongruentialEngine generators with well-chosen
parameters. MinstdRand0implements Park and Miller's "minimal
standard" generator that uses 16807 for the multiplier.MinstdRandimplements a variant that has slightly better spectral behavior by
using the multiplier 48271. Both generators are rather simplistic. | 
| Mt19937 | MersenneTwisterEngine!(uint,32L,624L,397L,31L,2567483615,11L,4294967295,7L,2636928640,15L,4022730752,18L,1812433253) | A MersenneTwisterEngineinstantiated with the parameters of the
original engine MT19937, generating uniformly-distributed 32-bit numbers with a
period of 2 to the power of 19937. Recommended for random number
generation unless memory is severely restricted, in which case aLinearCongruentialEnginewould be the generator of choice. | 
| Mt19937_64 | MersenneTwisterEngine!(ulong,64L,312L,156L,31L,-5403634167711393303L,29L,6148914691236517205L,17L,8202884508482404352L,37L,-2270628950310912L,43L,6364136223846793005L) | A MersenneTwisterEngineinstantiated with the parameters of the
original engine MT19937-64, generating uniformly-distributed 64-bit numbers with a
period of 2 to the power of 19937. | 
| Random | MersenneTwisterEngine!(uint,32L,624L,397L,31L,2567483615,11L,4294967295,7L,2636928640,15L,4022730752,18L,1812433253) | The "default", "favorite", "suggested" random number generator type on the current platform. It is an alias for one of the previously-defined generators. You may want to use it if (1) you need to generate some nice random numbers, and (2) you don't care for the minutiae of the method being used. | 
| unpredictableSeed | unpredictableSeed | A "good" seed for initializing random number engines. Initializing with unpredictableSeed makes engines generate different random number sequences every run. | 
| Xorshift | XorshiftEngine!(uint,128,11,-8,-19) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift128 | XorshiftEngine!(uint,128,11,-8,-19) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift160 | XorshiftEngine!(uint,160,2,-1,-4) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift192 | XorshiftEngine!(uint,192,-2,1,4) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift32 | XorshiftEngine!(uint,32,13,-17,15) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift64 | XorshiftEngine!(uint,64,10,-13,-10) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| Xorshift96 | XorshiftEngine!(uint,96,10,-5,-26) | Define XorshiftEnginegenerators with well-chosen parameters. See each bits examples of "Xorshift RNGs".Xorshiftis a Xorshift128's alias because 128bits implementation is mostly used. | 
| XorshiftEngine | XorshiftEngine!(UIntType,bits,a,-b,c) | Xorshift generator. Implemented according to Xorshift RNGs (Marsaglia, 2003) when the size is small. For larger sizes the generator uses Sebastino Vigna's optimization of using an index to avoid needing to rotate the internal array. | 
Authors
Andrei Alexandrescu Masahiro Nakagawa (Xorshift random generator) Joseph Rushton Wakeling (Algorithm D for random sampling) Ilya Yaroshenko (Mersenne Twister implementation, adapted from mir-random)