Last active
February 1, 2018 19:38
-
-
Save matthewhammer/b80f1fb2af0247e12cf713cc58340b36 to your computer and use it in GitHub Desktop.
Time with random points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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