Random variates over an explicit state (spec/07 §22), copied from visualize's
Visualize.Random, which read and advanced the calling process's :rand state.
Macaw.Draw holds no state, so every draw takes a state and answers the value with the
next state, {value, state}.
iex> alias Macaw.Draw.Random
iex> {a, state} = Random.uniform(Random.new(42))
iex> {b, _state} = Random.uniform(state)
iex> {a == elem(Random.uniform(Random.new(42)), 0), a != b}
{true, true}new(seed) is :rand.seed_s(:exsss, {s, 2s, 3s}), the state visualize's seed/1
installed in the process, so a seed draws the same values as visualize's did for every
distribution that consumes uniforms in the same order. A parameter of the wrong type is
{[name], {:type, :number}} and one out of range {[name], {:out_of_range, value}}, at
the parameter's name; a state that is no :rand state is {[:state], {:type, :state}}.
Summary
Types
A draw: the value and the next state, or the faults of its parameters.
A :rand state, as :rand.seed_s/2 makes it.
Functions
1 with probability p, else 0; p = 0.5.
x / (x + y), x = gamma(alpha) and y = gamma(beta).
The successes in n trials of probability p; 0 for n = 0.
location + scale · tan(π (u − 0.5)); location = 0, scale = 1.
−ln(1 − u) / lambda; lambda = 1.
Marsaglia–Tsang; a shape below 1 boosted through shape + 1; scale = 1.
The failures before the first success, trunc(ln(1 − u) / ln(1 − p)), p in (0, 1].
A point uniform over the disc, r = radius · √u; cx = cy = 0, radius = 1.
A point uniform in the rectangle from (x0, y0) to (x1, y1).
exp(normal(mu, sigma)); mu = 0, sigma = 1.
The state of an integer seed: :rand.seed_s(:exsss, {s, 2s, 3s}).
A Box–Muller normal variate; mean = 0, stddev = 1.
A point uniform on the circumference; cx = cy = 0, radius = 1.
1 / (1 − u)^(1 / alpha), at least 1; alpha = 1.
One element of a list, or nil for [].
Knuth's multiplication method.
n distinct elements of a list (fewer when it is shorter).
n calls of fun, a 1-arity function of the state answering {value, state}, each
given the state the last answered: {values, state}, [] for n ≤ 0.
A Fisher–Yates permutation of a list.
A float in [min, max); min = 0, max = 1.
An integer in [min, max].
scale · (−ln(1 − u))^(1 / shape); scale = 1.
Types
@type draw(value) :: {value, state()} | {:error, [Macaw.Base.Fault.t()]}
A draw: the value and the next state, or the faults of its parameters.
@type state() :: :rand.state()
A :rand state, as :rand.seed_s/2 makes it.
Functions
1 with probability p, else 0; p = 0.5.
x / (x + y), x = gamma(alpha) and y = gamma(beta).
@spec binomial(non_neg_integer(), number(), state()) :: draw(non_neg_integer())
The successes in n trials of probability p; 0 for n = 0.
location + scale · tan(π (u − 0.5)); location = 0, scale = 1.
−ln(1 − u) / lambda; lambda = 1.
Marsaglia–Tsang; a shape below 1 boosted through shape + 1; scale = 1.
@spec geometric(number(), state()) :: draw(non_neg_integer())
The failures before the first success, trunc(ln(1 − u) / ln(1 − p)), p in (0, 1].
A point uniform over the disc, r = radius · √u; cx = cy = 0, radius = 1.
A point uniform in the rectangle from (x0, y0) to (x1, y1).
exp(normal(mu, sigma)); mu = 0, sigma = 1.
@spec new(integer()) :: state() | {:error, [Macaw.Base.Fault.t()]}
The state of an integer seed: :rand.seed_s(:exsss, {s, 2s, 3s}).
A Box–Muller normal variate; mean = 0, stddev = 1.
A point uniform on the circumference; cx = cy = 0, radius = 1.
1 / (1 − u)^(1 / alpha), at least 1; alpha = 1.
One element of a list, or nil for [].
@spec poisson(number(), state()) :: draw(non_neg_integer())
Knuth's multiplication method.
n distinct elements of a list (fewer when it is shorter).
n calls of fun, a 1-arity function of the state answering {value, state}, each
given the state the last answered: {values, state}, [] for n ≤ 0.
A Fisher–Yates permutation of a list.
A float in [min, max); min = 0, max = 1.
An integer in [min, max].
scale · (−ln(1 − u))^(1 / shape); scale = 1.