Skip to content

Instantly share code, notes, and snippets.

@alexspurling
Last active September 15, 2015 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexspurling/51048d3935afe7427c69 to your computer and use it in GitHub Desktop.
Save alexspurling/51048d3935afe7427c69 to your computer and use it in GitHub Desktop.
Elm example for producing random values in a model
import Html exposing (div, button, text)
import Html.Events exposing (onClick)
import StartApp.Simple as StartApp
import Random exposing (Seed)
type alias Model = { seed : Seed, values : List Int }
initialModel : Model
initialModel =
{ seed = (Random.initialSeed 0),
values = [1, 2, 3]
}
view address model =
div []
[ button [ onClick address AnotherOne ] [ text "Give me more randoms" ]
, div [] [ text (toString model.values) ]
]
randomList : Seed -> (List Int, Seed)
randomList seed =
let
(numberOfItems, seed') = Random.generate (Random.int 0 30) seed
in
Random.generate (Random.list numberOfItems (Random.int 0 100)) seed'
type Action = AnotherOne
update : Action -> Model -> Model
update action model =
case action of
AnotherOne ->
let
(randoms, newSeed) = randomList model.seed
in
{ seed = newSeed, values = randoms }
main =
StartApp.start { model = initialModel, view = view, update = update }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment