qcheck/random

Random

The random module provides basic random value generators that can be used to define Generators.

They are mostly inteded for internal use or “advanced” manual construction of generators. In typical usage, you will probably not need to interact with these functions much, if at all. As such, they are currently mostly undocumented.

Types

pub opaque type Generator(a)

An opaque type representing a seed value used to initialize random generators.

pub opaque type Seed

Constants

pub const max_int: Int

Attempting to generate values below this limit will not lead to good random results.

pub const min_int: Int

Attempting to generate values below this limit will not lead to good random results.

Functions

pub fn bind(
  generator: Generator(a),
  f: fn(a) -> Generator(b),
) -> Generator(b)
pub fn choose(one: a, other: a) -> Generator(a)
pub fn constant(value: a) -> Generator(a)
pub fn float(from from: Float, to to: Float) -> Generator(Float)
pub fn float_weighted(
  first: #(Float, a),
  others: List(#(Float, a)),
) -> Generator(a)

Like weighted but uses Floats to specify the weights.

Generally you should prefer weighted as it is faster.

pub fn int(from from: Int, to to: Int) -> Generator(Int)
pub fn map(
  generator: Generator(a),
  fun: fn(a) -> b,
) -> Generator(b)
pub fn random_sample(generator: Generator(a)) -> a
pub fn random_seed() -> Seed

random_seed() creates a new randomly-generated seed. You can use it when you don’t care about having specifically reproducible results.

Example

Use a random seed for the Config.

let config =
  qcheck.default_config()
  |> qcheck.with_seed(qcheck.random_seed())
pub fn sample(generator: Generator(a), seed: Seed) -> a
pub fn seed(n: Int) -> Seed

seed(n) creates a new seed from the given integer, n`.

Example

Use a specific seed for the Config.

let config =
  qcheck.default_config()
  |> qcheck.with_seed(qcheck.seed(124))
pub fn step(generator: Generator(a), seed: Seed) -> #(a, Seed)
pub fn then(
  generator: Generator(a),
  f: fn(a) -> Generator(b),
) -> Generator(b)

then is an alias for bind.

pub fn to_random_yielder(generator: Generator(a)) -> Yielder(a)
pub fn to_yielder(
  generator: Generator(a),
  seed: Seed,
) -> Yielder(a)
pub fn uniform(first: a, others: List(a)) -> Generator(a)
pub fn weighted(
  first: #(Int, a),
  others: List(#(Int, a)),
) -> Generator(a)
Search Document