tango.math.random.Random

$(DDOC_SECTIONS Random number generators

$(DDOC_DESCRIPTION This is an attempt at having a good flexible and easy to use random number generator. ease of use:
template isFloat(T)
if T is a float

alias DefaultEngine = tango.math.random.engines.KissCmwc.KissCmwc!(32u, 987655670LU).KissCmwc;
The default engine, a reasonably collision free, with good statistical properties not easy to invert, and with a relatively small key (but not too small)

class RandomG(SourceT = DefaultEngine);
Class that represents a random number generator. Normally you should get random numbers either with call-like interface: auto r=new Random(); r(i)(j)(k); or with randomize r.randomize(i); r.randomize(j); r.randomize(k); if you use this you should be able to easily switch distribution later, as all distributions support this interface, and can be built on the top of RandomG auto r2=r.NormalSource!(float)(); r2(i)(j)(k); there are utility methods within random for the cases in which you do not want to build a special distribution for just a few numbers

this(bool randomInit = true);
Creates and seeds a new generator

RandomG seed();
if source.canSeed seeds the generator using the shared rand generator (use urandom directly if available?)

RandomG seed(scope uint delegate() seedSource);
if source.canSeed seeds the generator using the given source of uints

uint next();
uint next(uint to);
uint next(uint from, uint to);
RandomG!(Sync!DefaultEngine) instance();
compatibility with old Random, deprecate??

T uniform(T, bool boundCheck = true)();
uniform distribution on the whole range of integer types, and on the (0;1) range for floating point types. Floating point guarantees the initialization of the full mantissa, but due to rounding effects it might have *very* small dependence due to rounding effects on the least significant bit (in case of tie 0 is favored). if boundCheck is false in the floating point case bounds might be included (but with a lower propability than other numbers)

T uniformR(T, bool boundCheck = true)(T to);
uniform distribution on the range [0;to) for integer types, and on the (0;to) range for floating point types. Same caveat as uniform(T) apply

T uniformRSymm(T, bool boundCheck = true, bool excludeZero = isFloat!T)(T to, int iter = 2000);
uniform distribution on the range (-to;to) for integer types, and on the (-to;0)(0;to) range for floating point types if boundCheck is true. If boundCheck=false the range changes to [-to;0)u(0;to] with a slightly lower propability at the bounds for floating point numbers. excludeZero controls if 0 is excluded or not (by default float exclude it, ints no). Please note that the probability of 0 in floats is very small due Cannot be used on unsigned types.

In here there is probably one of the few cases where c handling of modulo of negative numbers is handy

T uniformR2(T, bool boundCheck = true)(T from, T to);
uniform distribution [from;to) for integers, and (from;to) for floating point numbers. if boundCheck is false the bounds are included in the floating point number distribution. the range for int and long is limited to only half the possible range (it could be worked around using long aritmethic for int, and doing a carry by hand for long, but I think it is seldomly needed, for int you are better off using long when needed)

T uniformEl(T)(const(T[]) arr);
returns a random element of the given array (which must be non empty)

U randomizeUniform(U, bool boundCheck)(ref U a);
randomizes the given array and returns it (for some types this is potentially more efficient, both from the use of random numbers and speedwise)

U randomizeUniformR(U, V, bool boundCheck = true)(ref U a, V to);
randomizes the given array and returns it (for some types this is potentially more efficient, both from the use of random numbers and speedwise)

U randomizeUniformR2(U, V, W, bool boundCheck = true)(ref U a, V from, W to);
randomizes the given variable and returns it (for some types this is potentially more efficient, both from the use of random numbers and speedwise)

U randomizeUniformRSymm(U, V, bool boundCheck = true, bool excludeZero = isFloat!(BaseTypeOfArrays!U))(ref U a, V to);
randomizes the given variable like uniformRSymm and returns it (for some types this is potentially more efficient, both from the use of random numbers and speedwise)

RandG spawn(RandG = RandomG)();
returns another (mostly indipendent, depending on seed size) random generator

struct UniformDistribution(T, bool boundCheck);
uniform distribution on the whole range for integers, and on (0;1) for floats with boundCheck=true this is equivalent to r itself, here just for completness

UniformDistribution opCall(U, S...)(ref U a, S args);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom();
returns a random number

U randomize(U)(ref U a);
initialize el

struct UniformRDistribution(T, bool boundCheck);
uniform distribution on the subrange [0;to) for integers, (0;to) for floats

UniformRDistribution create(RandomG r, T to);
initializes the probability distribution

UniformRDistribution opCall(U)(ref U a);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom();
returns a random number

U randomize(U)(ref U a);
initialize el

struct UniformRSymmDistribution(T, bool boundCheck = true, bool excludeZero = isFloat!T);
uniform distribution on the subrange (-to;to) for integers, (-to;0)u(0;to) for floats excludeZero controls if the zero should be excluded, boundCheck if the boundary should be excluded for floats

UniformRSymmDistribution create(RandomG r, T to);
initializes the probability distribution

UniformRSymmDistribution opCall(U)(ref U a);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom();
returns a random number

U randomize(U)(ref U a);
initialize el

struct UniformR2Distribution(T, bool boundCheck);
uniform distribution on the subrange (-to;to) for integers, (0;to) for floats

UniformR2Distribution create(RandomG r, T from, T to);
initializes the probability distribution

UniformR2Distribution opCall(U, S...)(ref U a, S args);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom();
returns a random number

U randomize(U)(ref U a);
initialize a

struct GammaDistribution(T);
gamma distribution f=x^(alpha-1)*exp(-x/theta)/(gamma(alpha)*theta^alpha) alpha has to be bigger than 1, for alpha<1 use gammaD(alpha)=gammaD(alpha+1)*pow(r.uniform!(T),1/alpha) from Marsaglia and Tsang, ACM Transaction on Mathematical Software, Vol. 26, N. 3 2000, p 363-372

GammaDistribution opCall(U, S...)(ref U a, S args);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom(T a = alpha, T t = theta);
returns a single random number

U randomize(U)(ref U b, T a = alpha, T t = theta);
initializes b with gamma distribued random numbers

U randomizeOp(U, S)(S delegate(T) op, ref U b, T a = alpha, T t = theta);
maps op on random numbers (of type T) and initializes b with it

NormalSource!(RandomG, T) normalSource(T)();
generators of normal numbers (sigma=1,mu=0) of the given type f=exp(-x*x/(2*sigma^2))/(sqrt(2 pi)*sigma)

ExpSource!(RandomG, T) expSource(T)();
generators of exp distribued numbers (beta=1) of the given type f=1/beta*exp(-x/beta)

NormalSource!(RandomG, T).NormalDistribution normalD(T)(T sigma = cast(T)1, T mu = cast(T)0);
generators of normal numbers with a different default sigma/mu f=exp(-x*x/(2*sigma^2))/(sqrt(2 pi)*sigma)

ExpSource!(RandomG, T).ExpDistribution expD(T)(T beta);
exponential distribued numbers with a different default beta f=1/beta*exp(-x/beta)

GammaDistribution!T gammaD(T)(T alpha = cast(T)1, T theta = cast(T)1);
gamma distribued numbers with the given default alpha

UniformDistribution!(T, true) uniformD(T)();
uniform distribution on the whole integer range, and on (0;1) for floats should return simply this??

UniformDistribution!(T, false) uniformBoundsD(T)();
uniform distribution on the whole integer range, and on [0;1] for floats

UniformRDistribution!(T, true) uniformRD(T)(T to);
uniform distribution [0;to) for ints, (0:to) for reals

UniformRDistribution!(T, false) uniformRBoundsD(T)(T to);
uniform distribution [0;to) for ints, [0:to] for reals

UniformRSymmDistribution!(T, true, isFloat!T) uniformRSymmD(T)(T to);
uniform distribution (-to;to) for ints and (-to;0)u(0;to) for reals

UniformRSymmDistribution!(T, false, isFloat!T) uniformRSymmBoundsD(T)(T to);
uniform distribution (-to;to) for ints and [-to;0)u(0;to] for reals

UniformR2Distribution!(T, true) uniformR2D(T)(T from, T to);
uniform distribution [from;to) for ints and (from;to) for reals

UniformR2Distribution!(T, false) uniformR2BoundsD(T)(T from, T to);
uniform distribution [from;to) for ints and [from;to] for reals

T normal(T)();
returns a normal distribued number

T normalSigma(T)(T sigma);
returns a normal distribued number with the given sigma

T normalSigmaMu(T)(T sigma, T mu);
returns a normal distribued number with the given sigma and mu

T exp(T)();
returns an exp distribued number

T expBeta(T)(T beta);
returns an exp distribued number with the given scale beta

T gamma(T)(T alpha = cast(T)1, T sigma = cast(T)1);
returns a gamma distribued number from Marsaglia and Tsang, ACM Transaction on Mathematical Software, Vol. 26, N. 3 2000, p 363-372

string toString();
writes the current status in a string

size_t fromString(const(char[]) s);
reads the current status from a string (that should have been trimmed) returns the number of chars read

RandomG opCall(U)(ref U a);
chainable call style initialization of variables (thorugh a call to randomize)

T getRandom(T)();
returns a random number

U randomize(U)(ref U a);
initialize el

alias Random = RandomG!().RandomG;
make the default random number generator type (a non threadsafe random number generator) easily available you can safely expect a new instance of this to be indipendent from all the others

alias RandomSync = RandomG!(Sync!(KissCmwc!(32u, 987655670LU))).RandomG;
default threadsafe random number generator type

static RandomSync rand;
shared locked (threadsafe) random number generator initialized with urandom if available, with time otherwise


Page generated by Ddoc. Copyright (c) 2008. Fawzi Mohamed