Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elm generate random number

Tags:

random

elm

I want to generate a random int between two values in elm. Something like this:

nb = random(0, 10)

I have read the doc and multiple post. The best answer was from this stackoverflow post

gen = Random.int 0 10
seed0 = Random.initialSeed 123456
Random.generate gen seed0

But the issue is it's always return the same value and it's not even an int it's something like this:

(7,Seed { state = State 645041272 40692, next = <function>, split = <function>, range = <function> })
: ( Int, Random.Seed )

So from the doc, it's better to use the current time for the seed. But how do I get it? Do I have to use signal for this ? is there not an easy way to get a timestamp?

I'm a lot confuse, I need to generate a random int for the seed so I can generate a random int. If not the random int generated is not random. I think I have misunderstood something ...

edit: I have also found this post but I didn't understand everything.

like image 350
BoumTAC Avatar asked Feb 06 '16 00:02

BoumTAC


2 Answers

Updated for 0.18

app =
  Html.programWithFlags
    { init = init
    , update = update
    , view = view
    , subscriptions = always Sub.none
    }

init : {startTime : Float} -> Model 
init {startTime} = 
    { blankModel | randomSeed = Random.initialSeed <| round startTime }

index.html

<script type="text/javascript">
    var yourPgm = Elm.fullscreen(Elm.Main, {startTime: Date.now()});
</script>

Original Answer

Random numbers are complex in pure programs, but this is how I do it in one of my games (using Elm Architecture):

Main.elm

startTimeSeed : Seed
startTimeSeed = Random.initialSeed <| round startTime

app =
  StartApp.start
    { init = (init 8 8 startTimeSeed, Effects.none)
    , update = update
    , view = view
    , inputs = []
    }

port startTime : Float

index.html

<script type="text/javascript">
    var yourPgm = Elm.fullscreen(Elm.Main, {startTime: Date.now()});
</script>

In other words pass the time stamp through port when you start the game

like image 184
Simon H Avatar answered Nov 02 '22 17:11

Simon H


it's always return[ing] the same value

This is how pure random number generators work. You pass in a Seed, and you get back another Seed.

and it's not even an int it's something like this [crazy code snippet]

This is a pair of values. The first is the int you're looking for. The second is the new seed to generate random values. Don't worry about what a seed actually is; it should be opaque. You can get the int out the pair using fst, but if you want more random numbers, you'll need the new seed.

So from the doc, it's better to use the current time for the seed.

This doc is wrong, wrong, wrong. As you've discovered, there isn't an easy way to get the current timestamp. And if you're passing it in from JS, like Simon advises, just use Math.floor(Math.random()*0xFFFFFFFF) instead. This gives you a seed that is better sampled over the possible input space.

This is extremely important because the random number generator will output similar values for similar seeds. For example, if you you use any seed less than 53668 and generate one bool, it will be True. This is because of weaknesses in the algorithm used.

The better solution: --> use this library<-- . It works the same way as the core library, but the algorithm is much better, and the docs on seeds aren't blatantly wrong.

like image 34
mgold Avatar answered Nov 02 '22 18:11

mgold