Elm - Random Number on init strange behaviour

John Perrin

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

http://guide.elm-lang.org/architecture/effects/random.html

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://guide.elm-lang.org/architecture/effects/random.html , 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 )
mariosangiorgio

This must to have something to do with seeding. You're not specifying any value for seed so the generator is defaulting to use the current time.

I think you tried to refresh your page for a few times in a few seconds and you didn't see the value change. If you wait for longer (roughly a minute) you'll see your value change.

I had a look at the source code of Random and I suspect that for seed values that are close enough the first value generated in the range [1,6] doesn't change. I'm not sure whether this is expected or not, probably it's worth raising an issue on GitHub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elm - Random Number on init strange behaviour

From Dev

What's causing this strange random number generator behaviour?

From Dev

Strange behaviour number

From Dev

Strange behaviour number

From Dev

Strange behaviour with Gaussian random distribution

From Dev

Strange behaviour of $parse, if passed string starts with number

From Dev

What is the expected behaviour of Random.initialSeed in elm (v0.15)

From Dev

Java strange behaviour when using double division in declaration/init

From Dev

Elm - producing a list of random number changing with time

From Dev

How can I compare random number in elm

From Dev

Strange random-seed behaviour after create-<breeds>-with

From Dev

Strange behaviour when using random.shuffle on numpy.array

From Dev

Strange random-seed behaviour after create-<breeds>-with

From Dev

Strange behaviour when using random.shuffle on numpy.array

From Dev

Strange behaviour with space in front of number value on Excel 2016 macOS

From Dev

Strange compiler error in Elm

From Dev

Strange addEventListener behaviour

From Dev

MATLAB subs(): Strange behaviour

From Dev

Strange behaviour of size utility

From Dev

IronPython strange behaviour

From Dev

htaccess issue - strange [OR] behaviour

From Dev

appendTo shows strange behaviour

From Dev

SQLite or JDBC strange behaviour

From Dev

Strange behaviour of jquery selector

From Dev

Strange bash behaviour

From Dev

Strange rand() behaviour in MATLAB

From Dev

Strange ModelViewSet behaviour

From Dev

UISearchBar subview strange behaviour

From Dev

Strange Order By behaviour with Union