This module provides a pseudo random number generator.
The module contains a number of algorithms.
The uniform distribution algorithms use the
For some algorithms, jump functions are provided for generating non-overlapping sequences for parallel computations. The jump functions perform calculations equivalent to perform a large number of repeated calls for calculating new states.
The following algorithms are provided:
Xoroshiro116+, 58 bits precision and period of 2^116-1
Jump function: equivalent to 2^64 calls
Xorshift1024*, 64 bits precision and a period of 2^1024-1
Jump function: equivalent to 2^512 calls
Xorshift116+, 58 bits precision and period of 2^116-1
Jump function: equivalent to 2^64 calls
This is a corrected version of the previous default algorithm,
that now has been superseded by Xoroshiro116+ (
The default algorithm is
Undocumented (old) algorithms are deprecated but still implemented so old code relying on them will produce the same pseudo random sequences as before.
There were a number of problems in the implementation of the now undocumented algorithms, which is why they are deprecated. The new algorithms are a bit slower but do not have these problems:
Uniform integer ranges had a skew in the probability distribution that was not noticable for small ranges but for large ranges less than the generator's precision the probability to produce a low number could be twice the probability for a high.
Uniform integer ranges larger than or equal to the generator's precision used a floating point fallback that only calculated with 52 bits which is smaller than the requested range and therefore were not all numbers in the requested range even possible to produce.
Uniform floats had a non-uniform density so small values i.e less than 0.5 had got smaller intervals decreasing as the generated value approached 0.0 although still uniformly distributed for sufficiently large subranges. The new algorithms produces uniformly distributed floats on the form N * 2.0^(-53) hence equally spaced.
Every time a random number is requested, a state is used to calculate it and a new state is produced. The state can either be implicit or be an explicit argument and return value.
The functions with implicit state use the process dictionary
variable
If a process calls
The functions with explicit state never use the process dictionary.
Examples:
Simple use; creates and seeds the default algorithm with a non-constant seed if not already done:
R0 = rand:uniform(), R1 = rand:uniform(),
Use a specified algorithm:
_ = rand:seed(exs1024s), R2 = rand:uniform(),
Use a specified algorithm with a constant seed:
_ = rand:seed(exs1024s, {123, 123534, 345345}), R3 = rand:uniform(),
Use the functional API with a non-constant seed:
S0 = rand:seed_s(exrop), {R4, S1} = rand:uniform_s(S0),
Textbook basic form Box-Muller standard normal deviate
R5 = rand:uniform_real(), R6 = rand:uniform(), SND0 = math:sqrt(-2 * math:log(R5)) * math:cos(math:pi() * R6)
Create a standard normal deviate:
{SND1, S2} = rand:normal_s(S1),
Create a normal deviate with mean -3 and variance 0.5:
{ND0, S3} = rand:normal_s(-3, 0.5, S2),
The builtin random number generator algorithms are not
cryptographically strong. If a cryptographically strong
random number generator is needed, use something like
For all these generators the lowest bit(s) has got
a slightly less random behaviour than all other bits.
1 bit for
Beside passing BigCrush, this generator passes the PractRand test suite up to (and included) 16TB, with the exception of binary rank tests, which fail due to the lowest bit being an LFSR; all other bits pass all tests. We suggest to use a sign test to extract a random Boolean value.
If this is a problem; to generate a boolean use something like this:
(rand:uniform(16) > 8)
And for a general range, with
(((rand:uniform(Range bsl N) - 1) bsr N) + 1)
The floating point generating functions in this module waste the lowest bits when converting from an integer so they avoid this snag.
Algorithm-dependent state.
Algorithm-dependent state that can be printed or saved to file.
Algorithm specific internal state
Algorithm specific internal state
Algorithm specific internal state
Algorithm specific internal state
Returns the random number state in an external format.
To be used with
Returns the random number generator state in an external format.
To be used with
Returns the state after performing jump calculation to the state in the process dictionary.
This function generates a
Returns the state after performing jump calculation to the given state.
This function generates a
Returns a standard normal deviate float (that is, the mean is 0 and the standard deviation is 1) and updates the state in the process dictionary.
Returns a normal N(Mean, Variance) deviate float and updates the state in the process dictionary.
Returns, for a specified state, a standard normal deviate float (that is, the mean is 0 and the standard deviation is 1) and a new state.
Returns, for a specified state, a normal N(Mean, Variance) deviate float and a new state.
Seeds random number generation with the specifed algorithm and
time-dependent data if
Otherwise recreates the exported seed in the process dictionary,
and returns the state. See also
Seeds random number generation with the specified algorithm and integers in the process dictionary and returns the state.
Seeds random number generation with the specifed algorithm and
time-dependent data if
Otherwise recreates the exported seed and returns the state.
See also
Seeds random number generation with the specified algorithm and integers and returns the state.
Returns a random float uniformly distributed in the value
range
The generated numbers are on the form N * 2.0^(-53), that is; equally spaced in the interval.
This function may return exactly
If neither endpoint is desired you can test and re-try like this:
my_uniform() -> case rand:uniform() of 0.0 -> my_uniform(); X -> X end end.
Returns a random float
uniformly distributed in the value range
Conceptually, a random real number
The generated numbers from this function has got better
granularity for small numbers than the regular
See
Returns, for a specified integer
Returns, for a specified state, random float
uniformly distributed in the value range
The generated numbers are on the form N * 2.0^(-53), that is; equally spaced in the interval.
This function may return exactly
If neither endpoint is desired you can test and re-try like this:
my_uniform(State) -> case rand:uniform(State) of {0.0, NewState} -> my_uniform(NewState); Result -> Result end end.
Returns, for a specified state, a random float
uniformly distributed in the value range
Conceptually, a random real number
The generated numbers from this function has got better
granularity for small numbers than the regular
The concept implicates that the probability to get
exactly zero is extremely low; so low that this function
is in fact guaranteed to never return zero. The smallest
number that it might return is
The value range stated at the top of this function
description is technically correct, but
For example; for all sub ranges
Having to generate extra random bits for
small numbers costs a little performance.
This function is about 20% slower than the regular
Returns, for a specified integer