Just working on the samples and getting the exercise of creating 2 random cubes and rolling them using the button.
http://guide.elm-lang.org/architecture/effects/random.html
So, I thought I would create the bones as a module, delete the roll action and just create the D6 value for init.
So my code is as follows (should open directly in the el reactor)
module Components.DiceRoller exposing (Model, Msg, init, update, view) import Html exposing (..) import Html.App as Html import Html.Attributes exposing (..) import Html.Events exposing (..) import Random import String exposing (..) main = Html.program { init = init , view = view , update = update , subscriptions = subscriptions } -- MODEL type alias Model = { dieFace : Int } init : ( Model, Cmd Msg ) init = ( Model 0, (Random.generate NewFace (Random.int 1 6)) ) -- UPDATE type Msg = NewFace Int update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of NewFace newFace -> ( Model newFace, Cmd.none ) -- SUBSCRIPTIONS subscriptions : Model -> Sub Msg subscriptions model = Sub.none -- VIEW dieFaceImage : Int -> String dieFaceImage dieFace = concat [ "/src/img/40px-Dice-", (toString dieFace), ".svg.png" ] view : Model -> Html Msg view model = let imagePath = dieFaceImage model.dieFace in div [] [ img [ src imagePath ] [] , span [] [ text imagePath ] ]
The problem is that it always produces the same value. I thought I was having a problem with the seed, but if you change
init = ( Model 0, (Random.generate NewFace (Random.int 1 6)) ) init = ( Model 0, (Random.generate NewFace (Random.int 1 100)) )
it works exactly as intended. So it looks like the default generator doesn't work with small values, it seems to work at least 10.
It's incredible that in this example (which I started with) http://guide.elm-lang.org/architecture/effects/random.html it works fine from 1-6 when it is not in init.
So my question is: am I doing something wrong, or is it just a wrinkle in the elms? Is my use of the command in init ok?
In the end, I put this to get the desired effect, which feels awkward.
init = ( Model 0, (Random.generate NewFace (Random.int 10 70)) )
from
NewFace newFace -> ( Model (newFace // 10), Cmd.none )