gwg_rng
This is a Random Number Generator (RNG) library for the Gleam programming language.
This package is a fork of prng.
This package can help you generate any kind of random values you can think of. It can be useful in many different scenarios: when you need to simulate non-deterministic actions, like rolling a dice or flipping a coin; when you need to write property based tests; or to make fun interactive games where you can spawn randomly generated enemies!
This package and its documentation are based on the awesome Elm implementation of Permuted Congruential Generators. They have great documentation full of clear and useful examples; if you are curious, give it a look and show Elm some love!
Installation
gleam add gwg_rng
import gwg/rng/random
pub fn main() {
// A generator describes which kind of random values to produce:
let generator = random.int(0, 10)
// One can take values out of a generator using the `step` function.
// Using the same initial seed will always produce the same value!
let #(value, _) = random.step(generator, random.new(11))
assert value == 10
let #(other_value, _) = random.step(generator, random.new(11))
assert other_value == 10
// The `step` function also produces a new seed you can use on successive
// calls to generate a new pseudo-random value each time you call it:
let #(value, next_seed) = random.step(generator, random.new(11))
assert value == 10
let #(other_value, _) = random.step(generator, next_seed)
assert other_value == 4
}
Further documentation can be found at https://hexdocs.pm/gwg_rng.
Credits
Special thanks to: