rand
(stdlib)Pseudo random number generation.
This module provides a random number generator. The module contains
a number of algorithms. The uniform distribution algorithms use the
The following algorithms are provided:
exsplus
Xorshift116+, 58 bits precision and period of 2^116-1
exs64
Xorshift64*, 64 bits precision and a period of 2^64-1
exs1024
Xorshift1024*, 64 bits precision and a period of 2^1024-1
The default algorithm is exsplus
. If a specific algorithm is
required, ensure to always use
seed/1
to initialize the state.
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 rand_seed
to remember the current state.
If a process calls
uniform/0
or
uniform/1
without
setting a seed first, seed/1
is called automatically with the default algorithm and creates a
non-constant seed.
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(exs1024), R2 = rand:uniform(),
Use a specified algorithm with a constant seed:
_ = rand:seed(exs1024, {123, 123534, 345345}), R3 = rand:uniform(),
Use the functional API with a non-constant seed:
S0 = rand:seed_s(exsplus), {R4, S1} = rand:uniform_s(S0),
Create a standard normal deviate:
{SND0, S2} = rand:normal_s(S1),
Note!
This random number generator is not cryptographically
strong. If a strong cryptographic random number generator is
needed, use one of functions in the
crypto
module, for example,
crypto:strong_rand_bytes/1
.
Types
alg/0
state/0
Algorithm-dependent state.
export_state/0
Algorithm-dependent state that can be printed or saved to file.
Functions
export_seed/0
Returns the random number state in an external format.
To be used with seed/1
.
export_seed_s/1
Returns the random number generator state in an external format.
To be used with seed/1
.
normal/0
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.
normal_s/1
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.
seed/1
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
export_seed/0
.
seed/2
Seeds random number generation with the specified algorithm and integers in the process dictionary and returns the state.
seed_s/1
Seeds random number generation with the specifed algorithm and
time-dependent data if
Otherwise recreates the exported seed and returns the state.
See also
export_seed/0
.
seed_s/2
Seeds random number generation with the specified algorithm and integers and returns the state.
uniform/0
Returns a random float uniformly distributed in the value
range 0.0 <
and
updates the state in the process dictionary.
uniform/1
Returns, for a specified integer
,
a random integer uniformly distributed in the value range
1 <=
and
updates the state in the process dictionary.
uniform_s/1
Returns, for a specified state, random float
uniformly distributed in the value range 0.0 <
and a new state.
uniform_s/2
Returns, for a specified integer
and a state, a random integer uniformly distributed in the value
range 1 <=
and a
new state.