module Web06RandomFloat exposing (..) -- import GrammarABequallyOften as ConcreteGrammar --import PrefixArithmeticGrammar as Grammar import Browser import Html exposing (Attribute, Html, button, div, input, text) import Html.Attributes as Att import Html.Events exposing (onClick, onInput) import Random type alias Model = { randomFloat : Maybe Float } type Msg = GenerateRandomFloat -- when the button has been clicked | RandomFloatReceived Float -- when the random number has arrived randomFloatGenerator : Random.Generator Float randomFloatGenerator = Random.float 0 1 randomFloatCmd : Cmd Msg randomFloatCmd = Random.generate RandomFloatReceived randomFloatGenerator update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of GenerateRandomFloat -> ( model, randomFloatCmd ) RandomFloatReceived number -> ( { model | randomFloat = Just number }, Cmd.none ) css path = Html.node "link" [ Att.rel "stylesheet", Att.href path ] [] view : Model -> Html Msg view model = div [ Att.class "container" ] [ css "https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" , Html.p [] [ Html.text "source can be found under ", Html.code [] [ Html.text "/elm-codebase/random/DemoRandom.elm" ] ] , Html.h2 [] [ Html.text "Demo for random numbers" ] , div [] [ Html.button [ Html.Events.onClick GenerateRandomFloat ] [ Html.text "Number!" ] , Html.text (case model.randomFloat of Nothing -> "No random float received yet" Just r -> "Number is " ++ String.fromFloat r ) ] ] init : () -> ( Model, Cmd Msg ) init _ = ( { randomFloat = Nothing } , Cmd.none ) main = Browser.element { init = init , view = view , update = update , subscriptions = \_ -> Sub.none -- subscriptions is stuff we want to listen to, beyond the input elements of our web app -- this could, for example, be web sockets ... }