elm-community/random-extra/2.0.0

NOTE: this package is not compatible with Elm 0.19.1

Random.Extra

This module provides many common and general-purpose helper functions for core's Random library. You can find even more useful functions for a particular type in the other modules.

Constant Generators

constant : a -> Generator a

Create a generator that always produces the value provided. This is useful when creating complicated chained generators and you need to handle a simple case. It's also useful for the base case of recursive generators.

Maps

For map and mapN up through N=5, use the core library.

(a -> b -> c -> d -> e -> f -> g)
-> Generator a
-> Generator b
-> Generator c
-> Generator d
-> Generator e
-> Generator f
-> Generator g

Map a function of six arguments over six generators.

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

Map over any number of generators.

randomPerson : Generator Person
randomPerson =
  map person genFirstName
    |> andMap genLastName
    |> andMap genBirthday
    |> andMap genPhoneNumber
    |> andMap genAddress
    |> andMap genEmail

New Generators

oneIn : Int -> Generator Bool

Produce True one-in-n times on average.

Do not pass a value less then one to this function.

flippedHeads = oneIn 2
rolled6 = oneIn 6
maybe : Generator Bool -> Generator a -> Generator (Maybe a)

Produce Just a value on True, and Nothing on False.

You can use bool or oneIn n for the first argument.

Generator Bool
-> Generator err
-> Generator val
-> Generator (Result err val)

Produce an Ok a value on True, and an Err value on False.

You can use bool or oneIn n for the first argument.

choice : a -> a -> Generator a

Choose between two values with equal probability.

type Flip = Heads | Tails


coinFlip : Generator Flip
coinFlip =
  choice Heads Tails

Note that this function takes values, not generators. That's because it's meant to be a lightweight helper for a specific use. If you need to choose between two generators, use choices [gen1, gen2].

Working with Lists

choices : List (Generator a) -> Generator a

Create a generator that chooses a generator from a list of generators with equal probability.

Warning: Do not pass an empty list or your program will crash! In practice this is usually not a problem since you pass a list literal.

frequency : List ( Float, Generator a ) -> Generator a

Create a generator that chooses a generator from a list of generators based on the provided weight. The likelihood of a given generator being chosen is its weight divided by the total weight (which doesn't have to equal 1).

Warning: Do not pass an empty list or your program will crash! In practice this is usually not a problem since you pass a list literal.

sample : List a -> Generator (Maybe a)

Given a list, choose an element uniformly at random. Nothing is only produced if the list is empty.

type Direction = North | South | East | West


direction : Generator Direction
direction =
  sample [North, South, East, West]
    |> map (Maybe.withDefault North)
combine : List (Generator a) -> Generator (List a)

Turn a list of generators into a generator of lists.

rangeLengthList : Int -> Int -> Generator a -> Generator (List a)

Generate a random list of random length given a minimum length and a maximum length.

Filtered Generators

filter : (a -> Bool) -> Generator a -> Generator a

Filter a generator so that all generated values satisfy the given predicate.

evens : Generator Int
evens =
  filter (\i -> i % 2 == 0) (int minInt maxInt)

Warning: If the predicate is unsatisfiable, the generator will not terminate, your application will crash with a stack overflow, and you will be sad. You should also avoid predicates that are merely very difficult to satisfy.

badCrashingGenerator =
  filter (\_ -> False) anotherGenerator


likelyCrashingGenerator =
  filter (\i -> i % 2000 == 0) (int minInt maxInt)

andThenN

These functions are like mapN except the function you pass in does not return an exact value, but instead another generator. That means you can take in several random arguments to drive more randomness.

andThen2 : (a -> b -> Generator c) -> Generator a -> Generator b -> Generator c
(a -> b -> c -> Generator d)
-> Generator a
-> Generator b
-> Generator c
-> Generator d
(a -> b -> c -> d -> Generator e)
-> Generator a
-> Generator b
-> Generator c
-> Generator d
-> Generator e
(a -> b -> c -> d -> e -> Generator f)
-> Generator a
-> Generator b
-> Generator c
-> Generator d
-> Generator e
-> Generator f
(a -> b -> c -> d -> e -> f -> Generator g)
-> Generator a
-> Generator b
-> Generator c
-> Generator d
-> Generator e
-> Generator f
-> Generator g