rand
(stdlib)Pseudo random number generation
Random number generator.
The module contains several different algorithms and can be
extended with more in the future. The current uniform
distribution algorithms uses the
The implemented algorithms are:
exsplus
exs64
exs1024
The current default algorithm is exsplus
. The default
may change in future. If a specific algorithm is required make
sure 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 produced. The state can either be implicit or it can 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 usage. Creates and seeds the default algorithm %% with a non-constant seed if not already done. R0 = rand:uniform(), R1 = rand:uniform(), %% Use a given algorithm. _ = rand:seed(exs1024), R2 = rand:uniform(), %% Use a given algorithm with a constant seed. _ = rand:seed(exs1024, {123, 123534, 345345}), R3 = rand:uniform(), %% Use the functional api with 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:rand_bytes/1
.
Types
alg() = exs64 | exsplus | exs1024
state()
Algorithm dependent state.
export_state()
Algorithm dependent state which can be printed or saved to file.
Functions
seed(AlgOrExpState :: alg() | export_state()) -> state()
Seeds random number generation with the given 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_s(AlgOrExpState :: alg() | export_state()) -> state()
Seeds random number generation with the given algorithm and time dependent
data if
Otherwise recreates the exported seed and returns the state. See also: export_seed/0.
seed(Alg :: alg(), S0 :: {integer(), integer(), integer()}) ->
state()
Seeds random number generation with the given algorithm and integers in the process dictionary and returns the state.
seed_s(Alg :: alg(), S0 :: {integer(), integer(), integer()}) ->
state()
Seeds random number generation with the given algorithm and integers and returns the state.
export_seed() -> undefined | export_state()
Returns the random number state in an external format. To be used with seed/1.
export_seed_s(X1 :: state()) -> export_state()
Returns the random number generator state in an external format. To be used with seed/1.
uniform() -> X :: float()
Returns a random float uniformly distributed in the value
range 0.0 <
and
updates the state in the process dictionary.
uniform_s(State :: state()) -> {X :: float(), NewS :: state()}
Given a state, uniform_s/1
returns a random float
uniformly distributed in the value range 0.0 <
and a new state.
uniform(N :: integer() >= 1) -> X :: integer() >= 1
Given an integer
,
uniform/1
returns a random integer uniformly
distributed in the value range
1 <=
and
updates the state in the process dictionary.
uniform_s(N :: integer() >= 1, State :: state()) ->
{X :: integer() >= 1, NewS :: state()}
Given an integer
and a state,
uniform_s/2
returns a random integer uniformly
distributed in the value range 1 <=
and a new state.
normal() -> float()
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.