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.
For map and mapN up through N=5, use the core library.
Map a function of six arguments over six generators.
Map over any number of generators.
randomPerson : Generator Person
randomPerson =
map person genFirstName
|> andMap genLastName
|> andMap genBirthday
|> andMap genPhoneNumber
|> andMap genAddress
|> andMap genEmailProduce True one-in-n times on average.
Do not pass a value less then one to this function.
flippedHeads = oneIn 2
rolled6 = oneIn 6Produce Just a value on True, and Nothing on False.
You can use bool or oneIn n for the first argument.
Produce an Ok a value on True, and an Err value on False.
You can use bool or oneIn n for the first argument.
Choose between two values with equal probability.
type Flip = Heads | Tails
coinFlip : Generator Flip
coinFlip =
choice Heads TailsNote 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].
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.
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.
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)Turn a list of generators into a generator of lists.
Generate a random list of random length given a minimum length and a maximum length.
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)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.
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.