lundi 30 mai 2016

Elm - Random Number on init strange behaviour

Just working through the samples, and got the exercise about creating 2 random dice and rolling them with a button.

http://ift.tt/1OXsKk5

So I thought I would create the dice as a module, remove the roll action, and just have it create a D6 value on init.

So my code is now as follows (should open direct in elm-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 with this is that it is always producing the same value. I thought I had a problem with the seed to begin with, 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 is not working with small values, seems to work as low down as 10.

The odd thing is this, in this example (which i started with) http://ift.tt/1OXsKk5 , it works fine with 1-6 when it's not in init.

So my question is, am I doing something wrong, or is this just a wrinkle in elm? Is my usage of the command in init ok?

In the end, I put this in to get the desired effect, which feels wonky.

init =
    ( Model 0, (Random.generate NewFace (Random.int 10 70)) )

with

    NewFace newFace ->
        ( Model (newFace // 10), Cmd.none )




Aucun commentaire:

Enregistrer un commentaire