New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with small values in Random.int on init #635

Closed
PezzA opened this Issue Jun 2, 2016 · 9 comments

Comments

Projects
None yet
8 participants
@PezzA

PezzA commented Jun 2, 2016

Raised this question on stack overflow and it was suggested to create a bug report here.

http://stackoverflow.com/questions/37522369/elm-random-number-on-init-strange-behaviour

I've been working through the examples here:

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

And what I'm trying to do with the dice is to get to generate a D6 when then component is created.


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 ]
            ]

However if I use 1 6 as the random min max, I only get 1 different value per minute. This only happens on init, it's fine in the orig example where it a UI action.

@mariosangiorgio

This comment has been minimized.

Show comment
Hide comment
@mariosangiorgio

mariosangiorgio Jun 2, 2016

Contributor

As I mentioned on the StackOverflow answer, this behaviour is a bit odd but I am not sure if that's expected or not. What is happening is that, for this combination of parameters, there are quite big intervals of seeds that generate the same first value of the pseudo-random sequence.

It's worth noting that, if we exclude the first value, the sequence of the numbers produced look random enough.

I had another look at the implementation of the random number generator, comparing it both to the Haskell implementation and to the original paper. I found that one of the magic numbers might have a wrong value.

We have magicNum7 = 2137383399 but I think it should be magicNum7 = 2147483399. Updating it is not affecting this particular issue, but it's still worth amending the constant to the correct value.

Contributor

mariosangiorgio commented Jun 2, 2016

As I mentioned on the StackOverflow answer, this behaviour is a bit odd but I am not sure if that's expected or not. What is happening is that, for this combination of parameters, there are quite big intervals of seeds that generate the same first value of the pseudo-random sequence.

It's worth noting that, if we exclude the first value, the sequence of the numbers produced look random enough.

I had another look at the implementation of the random number generator, comparing it both to the Haskell implementation and to the original paper. I found that one of the magic numbers might have a wrong value.

We have magicNum7 = 2137383399 but I think it should be magicNum7 = 2147483399. Updating it is not affecting this particular issue, but it's still worth amending the constant to the correct value.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jul 25, 2016

Contributor

@PezzA, @mariosangiorgio, in case you are still wondering what the issue here is: It's not the "magic numbers" thing, it's the fact that the randome generator is seeded from the current time at program start. See https://github.com/elm-lang/core/issues/673 for more explanation.

Contributor

jvoigtlaender commented Jul 25, 2016

@PezzA, @mariosangiorgio, in case you are still wondering what the issue here is: It's not the "magic numbers" thing, it's the fact that the randome generator is seeded from the current time at program start. See https://github.com/elm-lang/core/issues/673 for more explanation.

@evancz evancz referenced this issue Sep 22, 2016

Closed

Random #724

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Sep 22, 2016

Member

Not sure exactly what's wrong here, but maybe @mgold does. In any case, added this to #724 for tracking anything related to Random.

Member

evancz commented Sep 22, 2016

Not sure exactly what's wrong here, but maybe @mgold does. In any case, added this to #724 for tracking anything related to Random.

@evancz evancz closed this Sep 22, 2016

@mgold

This comment has been minimized.

Show comment
Hide comment
@mgold

mgold Sep 23, 2016

Contributor

This will almost certainly be resolved by the new RNG.

Contributor

mgold commented Sep 23, 2016

This will almost certainly be resolved by the new RNG.

@connec

This comment has been minimized.

Show comment
Hide comment
@connec

connec Dec 17, 2016

I also encountered this working through the Random section in the guide.

I think it's interesting (though probably not valuable) to observe that if "Initialize the dice to a random value" was added as an exercise, it would probably have to be removed or else it would generate a lot of confusion.

connec commented Dec 17, 2016

I also encountered this working through the Random section in the guide.

I think it's interesting (though probably not valuable) to observe that if "Initialize the dice to a random value" was added as an exercise, it would probably have to be removed or else it would generate a lot of confusion.

@seethroughtrees

This comment has been minimized.

Show comment
Hide comment
@seethroughtrees

seethroughtrees Jun 23, 2017

I've encountered this same issue. It seems if I use Random.Int with 3 or less. I will get repeated numbers in the same fashion as seen above. Anything over 3, and I get random as expected.

Like the above, this only occurs when triggered on init. Is there a workaround here?

I can create and provide an example to reproduce or versions if necessary.

seethroughtrees commented Jun 23, 2017

I've encountered this same issue. It seems if I use Random.Int with 3 or less. I will get repeated numbers in the same fashion as seen above. Anything over 3, and I get random as expected.

Like the above, this only occurs when triggered on init. Is there a workaround here?

I can create and provide an example to reproduce or versions if necessary.

@mgold

This comment has been minimized.

Show comment
Hide comment
@mgold

mgold Jun 25, 2017

Contributor

BTW that new RNG is #778. Until it's merged and released, use my library linked just above.

Contributor

mgold commented Jun 25, 2017

BTW that new RNG is #778. Until it's merged and released, use my library linked just above.

@seethroughtrees

This comment has been minimized.

Show comment
Hide comment
@seethroughtrees

seethroughtrees Jun 25, 2017

Perfect. Thanks for the update @lydell and @mgold . Good to have that link in this thread too.

seethroughtrees commented Jun 25, 2017

Perfect. Thanks for the update @lydell and @mgold . Good to have that link in this thread too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment