# `Macaw.Draw.Random`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/random.ex#L1)

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}}`.

# `draw`

```elixir
@type draw(value) :: {value, state()} | {:error, [Macaw.Base.Fault.t()]}
```

A draw: the value and the next state, or the faults of its parameters.

# `state`

```elixir
@type state() :: :rand.state()
```

A `:rand` state, as `:rand.seed_s/2` makes it.

# `bernoulli`

```elixir
@spec bernoulli(number(), state()) :: draw(0 | 1)
```

`1` with probability `p`, else `0`; `p = 0.5`.

# `beta`

```elixir
@spec beta(number(), number(), state()) :: draw(float())
```

`x / (x + y)`, `x = gamma(alpha)` and `y = gamma(beta)`.

# `binomial`

```elixir
@spec binomial(non_neg_integer(), number(), state()) :: draw(non_neg_integer())
```

The successes in `n` trials of probability `p`; `0` for `n = 0`.

# `cauchy`

```elixir
@spec cauchy(number(), number(), state()) :: draw(float())
```

`location + scale · tan(π (u − 0.5))`; `location = 0`, `scale = 1`.

# `exponential`

```elixir
@spec exponential(number(), state()) :: draw(float())
```

`−ln(1 − u) / lambda`; `lambda = 1`.

# `gamma`

```elixir
@spec gamma(number(), number(), state()) :: draw(float())
```

Marsaglia–Tsang; a shape below 1 boosted through `shape + 1`; `scale = 1`.

# `geometric`

```elixir
@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]`.

# `in_circle`

```elixir
@spec in_circle(number(), number(), number(), state()) :: draw({float(), float()})
```

A point uniform over the disc, `r = radius · √u`; `cx = cy = 0`, `radius = 1`.

# `in_rect`

```elixir
@spec in_rect(number(), number(), number(), number(), state()) ::
  draw({float(), float()})
```

A point uniform in the rectangle from `(x0, y0)` to `(x1, y1)`.

# `log_normal`

```elixir
@spec log_normal(number(), number(), state()) :: draw(float())
```

`exp(normal(mu, sigma))`; `mu = 0`, `sigma = 1`.

# `new`

```elixir
@spec new(integer()) :: state() | {:error, [Macaw.Base.Fault.t()]}
```

The state of an integer seed: `:rand.seed_s(:exsss, {s, 2s, 3s})`.

# `normal`

```elixir
@spec normal(number(), number(), state()) :: draw(float())
```

A Box–Muller normal variate; `mean = 0`, `stddev = 1`.

# `on_circle`

```elixir
@spec on_circle(number(), number(), number(), state()) :: draw({float(), float()})
```

A point uniform on the circumference; `cx = cy = 0`, `radius = 1`.

# `pareto`

```elixir
@spec pareto(number(), state()) :: draw(float())
```

`1 / (1 − u)^(1 / alpha)`, at least 1; `alpha = 1`.

# `pick`

```elixir
@spec pick([term()], state()) :: draw(term())
```

One element of a list, or `nil` for `[]`.

# `poisson`

```elixir
@spec poisson(number(), state()) :: draw(non_neg_integer())
```

Knuth's multiplication method.

# `sample`

```elixir
@spec sample([term()], integer(), state()) :: draw([term()])
```

`n` distinct elements of a list (fewer when it is shorter).

# `samples`

```elixir
@spec samples((state() -&gt; {term(), state()}), integer(), state()) :: draw([term()])
```

`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`.

# `shuffle`

```elixir
@spec shuffle([term()], state()) :: draw([term()])
```

A Fisher–Yates permutation of a list.

# `uniform`

```elixir
@spec uniform(number(), number(), state()) :: draw(float())
```

A float in `[min, max)`; `min = 0`, `max = 1`.

# `uniform_int`

```elixir
@spec uniform_int(integer(), integer(), state()) :: draw(integer())
```

An integer in `[min, max]`.

# `weibull`

```elixir
@spec weibull(number(), number(), state()) :: draw(float())
```

`scale · (−ln(1 − u))^(1 / shape)`; `scale = 1`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
