Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Time with random points
import Html exposing (Html, div, text)
import Time exposing (Time, second)
import Random exposing (Seed)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model = {
seed: Seed,
points: List Point
}
type alias Point = {x:Float, y:Float}
initialModel =
{seed = Random.initialSeed 0,
points = []}
init : (Model, Cmd Msg)
init = (initialModel, Cmd.none)
-- UPDATE
type alias Msg = Float
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
let (p, s) = genPoint3 model.seed in
({seed=s, points = p :: model.points }, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every second identity
genFloats : Float -> (Float, Float)
genFloats seed =
let g = Random.float 0.0 100.0 in
let s = Random.initialSeed (round seed) in
let (r1, s2) = Random.step g s in
let (r2, s3) = Random.step g s2 in
(r1, r2)
genPoint3 : Seed -> (Point, Seed)
genPoint3 seed =
let gf = (Random.float 0.0 100.0) in
let gp = Random.map2 (\x -> \y -> {x=x,y=y}) gf gf in
let (p, s2) = Random.step gp seed in
(p, s2)
-- VIEW
view : Model -> Html Msg
view model =
let (p, _) = genPoint3 model.seed in
div [] [text (toString model), text (" ("), text (toString p.x), text (", "), text (toString p.y), text (")")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.