elm-lang/core/2.1.0

NOTE: this package moved to elm/core

NOTE: this package is not compatible with Elm 0.19.1

Random

This library helps you generate pseudo-random values.

The general pattern is to define a Generator which can produce certain kinds of random values. You actually produce random values by feeding a fresh Seed to your Generator.

Since you need a fresh Seed to produce more random values, you should probably store a Seed in your application's state. This will allow you to keep updating it as you generate random values and fresh seeds.

The following example models a bunch of bad guys that randomly appear. The possiblyAddBadGuy function uses the random seed to see if we should add a bad guy, and if so, it places a bad guy at a randomly generated point.

type alias Model =
    { badGuys : List (Float,Float)
    , seed : Seed
    }


possiblyAddBadGuy : Model -> Model
possiblyAddBadGuy model =
    let (addProbability, seed') =
          generate (float 0 1) model.seed
    in
        if addProbability < 0.9
          then
            { model |
                seed <- seed'
            }
          else
            let (position, seed'') =
                  generate (pair (float 0 100) (float 0 100)) seed'
            in
                { model |
                    badGuys <- position :: model.badGuys
                    seed <- seed''
                }

Details: This is an implemenation of the Portable Combined Generator of L'Ecuyer for 32-bit computers. It is almost a direct translation from the System.Random module. It has a period of roughly 2.30584e18.

Generators

int : Int -> Int -> Generator Int

Create a generator that produces 32-bit integers in a given range. This function can produce values outside of the range [minInt, maxInt] but sufficient randomness is not guaranteed.

int 0 10   -- an integer between zero and ten
int -5 5   -- an integer between -5 and 5


int minInt maxInt  -- an integer in the widest range feasible
float : Float -> Float -> Generator Float

Create a generator that produces floats in a given range.

probability : Generator Float
probability =
    float 0 1


-- generate probability seed0 ==> (0.51, seed1)
-- generate probability seed1 ==> (0.04, seed2)
pair : Generator a -> Generator b -> Generator ( a, b )

Create a pair of random values. A common use of this might be to generate a point in a certain 2D space. Imagine we have a collage that is 400 pixels wide and 200 pixels tall.

randomPoint : Generator (Int,Int)
randomPoint =
    pair (int -200 200) (int -100 100)
list : Int -> Generator a -> Generator (List a)

Create a list of random values.

floatList : Generator (List Float)
floatList =
    list 10 (float 0 1)


intList : Generator (List Int)
intList =
    list 5 (int 0 100)


intPairs : Generator (List (Int, Int))
intPairs =
    list 10 (pair int int)

Running a Generator

generate : Generator a -> Seed -> ( a, Seed )

Run a random value generator with a given seed. It will give you back a random value and a new seed.

seed0 = initialSeed 31415


-- generate (int 0 100) seed0 ==> (42, seed1)
-- generate (int 0 100) seed1 ==> (31, seed2)
-- generate (int 0 100) seed2 ==> (99, seed3)

Notice that we use different seeds on each line. This is important! If you use the same seed, you get the same results.

-- generate (int 0 100) seed0 ==> (42, seed1)
-- generate (int 0 100) seed0 ==> (42, seed1)
-- generate (int 0 100) seed0 ==> (42, seed1)
initialSeed : Int -> Seed

Create a “seed” of randomness which makes it possible to generate random values. If you use the same seed many times, it will result in the same thing every time! A good way to get an unexpected seed is to use the current time.

Constants

maxInt : Int

The maximum value for randomly generated 32-bit ints.

minInt : Int

The minimum value for randomly generated 32-bit ints.

Custom Generators

customGenerator : (Seed -> ( a, Seed )) -> Generator a

Create a custom generator. You provide a function that takes a seed, and returns a random value and a new seed. You can use this to create custom generators not covered by the basic functions in this library.

pairOf : Generator a -> Generator (a,a)
pairOf generator =
  customGenerator <| \seed ->
    let (left , seed' ) = generate generator seed
        (right, seed'') = generate generator seed'
    in
        ((left,right), seed'')