Elm - random number when starting strange behavior

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 ) 
+6
source share
1 answer

This should have something to do with sowing. You do not specify any value for the seed, so the generator uses the current time by default.

I think you tried to refresh your page several times in a few seconds, and you did not see the value change. If you wait longer (about a minute), you will see a change in your value.

I looked at the source code of Random , and I suspect that for initial values ​​that are close enough, the first value generated in the range [1,6] does not change. I'm not sure if this was expected or not, maybe it’s worth raising a question about GitHub

+3
source

All Articles