gleam_stats/rand
A module for generating random numbers from common distributions.
- Retrieving numbers from the base-iterator
- Generating random numbers from common distributions
Functions
pub fn next_bern(stream: Iterator(Int), p: Float, m: Int) -> #(
List(Int),
Iterator(Int),
)
Generate ‘m’ random numbers from a bernoulli distribution with parameter 0 ≤ ‘p’ ≤ 1.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let p: Float = 0.5.
let out =
generators.seed_mt19937(seed)
|> rand.next_bern(p, 5_000)
let randberns: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_binom(stream: Iterator(Int), p: Float, n: Int, m: Int) -> #(
List(Int),
Iterator(Int),
)
Generate ‘m’ random numbers from a binomial distribution with parameters 0 ≤ ‘p’ ≤ 1 and ‘n’ in {0, 1, 2,…}.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let p: Float = 0.5.
let n: Int = 5
let out =
generators.seed_mt19937(seed)
|> rand.next_binom(p, n, 5_000)
let randbinoms: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_exp(stream: Iterator(Int), lambda: Float, m: Int) -> #(
List(Float),
Iterator(Int),
)
Generate random numbers from an exponential distribution with parameter 0 < ‘lambda’.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let lambda: Float = 0.5
let out =
generators.seed_mt19937(seed)
|> rand.next_exp(lambda, 5_000)
let randexps: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_geom(stream: Iterator(Int), p: Float, m: Int) -> #(
List(Int),
Iterator(Int),
)
Generate ‘m’ random numbers from a geometric distribution with parameter 0 ≤ ‘p’ ≤ 1.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let p: Float = 0.5.
let out =
generators.seed_mt19937(seed)
|> rand.next_geom(p, 5_000)
let randgeoms: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_negbinom(stream: Iterator(Int), p: Float, n: Int, m: Int) -> #(
List(Int),
Iterator(Int),
)
Generate ‘m’ random numbers from a negative binomial distribution with parameters 0 ≤ ‘p’ ≤ 1 and ‘n’ in {0, 1, 2,…}.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let p: Float = 0.5.
let n: Int = 5
let out =
generators.seed_mt19937(seed)
|> rand.next_negbinom(p, n, 5_000)
let randnegbinoms: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_normal(stream: Iterator(Int), mean: Float, sigma: Float, m: Int) -> #(
List(Float),
Iterator(Int),
)
Generate ‘m’ random numbers from a normal distribution with a given mean and standard deviation.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let mean: Float = 0.
let std: Float = 1.
let out =
generators.seed_mt19937(seed)
|> rand.next_normal(mean, std, 5_000)
let randnorm: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_randint(stream: Iterator(Int), min: Int, max: Int, m: Int) -> #(
List(Int),
Iterator(Int),
)
Generate ‘m’ random numbers in the interval [‘min’, ‘max’] from a discrete uniform distribution (‘max’ included).
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let min: Int = 0.
let max: Int = 10.
let out =
generators.seed_mt19937(seed)
|> rand.next_randint(min, max, 5_000)
let randints: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn next_uniform(stream: Iterator(Int), min: Float, max: Float, m: Int) -> #(
List(Float),
Iterator(Int),
)
Generate ‘m’ random numbers in the interval ‘[min, max)’ from a continuous uniform distribution (‘max’ excluded).
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let min: Float = 0.
let max: Float = 1.
let out =
generators.seed_mt19937(seed)
|> rand.next_uniform(min, max, 5_000)
let randunif: List(Float) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}
pub fn take_randints(stream: Iterator(Int), m: Int) -> #(
List(Int),
Iterator(Int),
)
Take out ‘m’ integers from the raw stream of pseudo-random numbers produced by a base-iterator.
Example:
import gleam/iterator.{Iterator}
import gleam_stats/generator
import gleam_stats/rand
pub fn example() {
let seed: Int = 5
let out =
generators.seed_mt19937(seed)
|> rand.take_randints(5_000)
let randints: List(Int) = pair.first(out)
let stream: Iterator(Int) = pair.second(out)
}