NoRedInk / elm-random-general / Random.General

A basis for random number generators. You only need this if you want to use another internal random number generators than the default. This could for instance be used to implement a cryptographically secure random number generator, with the same functionality that you love from the elm Random library (e.g. Generators).

** All functions that are not documented here are exactly like the ones described in the Random.Pcg module. **

Create custom RNGs


type Config s

A Config describes how the internal random number generator works. see makeConfig for more.

makeConfig : (s -> s) -> (s -> Basics.Int) -> Config s

makeConfig takes two arguments, a next function and a peel function. The next function describes how to generate a new internal state from the current internal state. The peel function describes how to map the internal state to a single integer.

As an example, let's see how to implement a terrible RNG:

incrementRNG =
    makeConfig
        (\s ->
            -- Bitwise.shiftRightZfBy 0 is used to truncate the state to 32 bits
            s + 1 |> Bitwise.shiftRightZfBy 0
        )
        identity

Basic Generators


type Generator s a

step : Generator s a -> s -> ( a, s )

bool : Config s -> Generator s Basics.Bool

int : Config s -> Basics.Int -> Basics.Int -> Generator s Basics.Int

float : Config s -> Basics.Float -> Basics.Float -> Generator s Basics.Float

oneIn : Config s -> Basics.Int -> Generator s Basics.Bool

sample : Config s -> List a -> Generator s (Maybe a)

Combining Generators

pair : Generator s a -> Generator s b -> Generator s ( a, b )

list : Basics.Int -> Generator s a -> Generator s (List a)

maybe : Generator s Basics.Bool -> Generator s a -> Generator s (Maybe a)

choice : Config s -> a -> a -> Generator s a

choices : Config s -> Generator s a -> List (Generator s a) -> Generator s a

frequency : Config s -> ( Basics.Float, Generator s a ) -> List ( Basics.Float, Generator s a ) -> Generator s a

Custom Generators

constant : a -> Generator s a

map : (a -> b) -> Generator s a -> Generator s b

map2 : (a -> b -> c) -> Generator s a -> Generator s b -> Generator s c

map3 : (a -> b -> c -> d) -> Generator s a -> Generator s b -> Generator s c -> Generator s d

map4 : (a -> b -> c -> d -> e) -> Generator s a -> Generator s b -> Generator s c -> Generator s d -> Generator s e

map5 : (a -> b -> c -> d -> e -> f) -> Generator s a -> Generator s b -> Generator s c -> Generator s d -> Generator s e -> Generator s f

Combine five generators.

andMap : Generator s a -> Generator s (a -> b) -> Generator s b

andThen : (a -> Generator s b) -> Generator s a -> Generator s b

filter : (a -> Basics.Bool) -> Generator s a -> Generator s a

Constants

minInt : Basics.Int

maxInt : Basics.Int

Internal

generator : (s -> ( a, s )) -> Generator s a

This allows you to create base generators such as the int or float generator. Only use this if you really know what you are doing. In almost all cases it is better to create generators that are based on some other generators using the combinations functions provided in this library.

If you really need this, a generator is just a function of type s -> (a, s). E.g. a function that takes the current state of your seed, gets some value out of it and generates a new seed.