Skip to content

Instantly share code, notes, and snippets.

@boxdot
Last active August 29, 2015 14:25
Show Gist options
  • Save boxdot/bd78f6d0e371c7cc691f to your computer and use it in GitHub Desktop.
Save boxdot/bd78f6d0e371c7cc691f to your computer and use it in GitHub Desktop.
Using Elm architecture and setting random seed.
import Time
import Random
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- MODEL
type alias Model =
{ random : Int
, seed : Random.Seed
}
init = { random = 0, seed = Random.initialSeed 0 }
-- UPDATE
type Action = GenRandom
update : Action -> Model -> Model
update action model =
case action of
GenRandom ->
let
(random, seed') = Random.generate (Random.int 0 100) model.seed
in
{ model | random <- random, seed <- seed' }
-- VIEW
view : Signal.Address Action -> Model -> Html
view address model =
div []
[ text (toString model.random)
, text (toString model.seed)
, hr [] []
, button [ onClick address GenRandom ] [ text "Generate random" ]
]
-- WIRING
main =
let
action = Signal.mailbox Nothing
address = Signal.forwardTo action.address Just
currentTime = Time.every Time.second
model = Signal.foldp
(\(Just action) model -> update action model)
init
action.signal
model' = Signal.map2
(\model time -> { model | seed <- Random.initialSeed (round time) })
model
currentTime
in
Signal.map (view address) model'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment