The documentation for Elm's Random
module states:
A good way to get an unexpected seed is to use the current time. http://package.elm-lang.org/packages/elm-lang/core/1.1.0/Random
I don't see however a good example of how to perform such initialization logic in FRP application. What signal should I react to? How to do this with a minimum of code and maximum of clarity.
If you are using the StartApp then you'll need to use a custom HTML file with
Then to use the startTime as a seed:
And then in the code you'll be doing something like
where, for example:
I reworked the third example from @Apanatshka above, trying to get to simpler code that feels more like the standard architecture, at least as seen in Mike Clark's training videos, and runs under Elm 0.16. Here is the refactored version I came up with:
This needs special help in the HTML file, so here's a file containing two examples:
There are different ways to do this. Each has it's own benefits. I'll give you the three that I know with a similar example for each.
1) Add a time ticker input
One thing you could do is add time to the inputs of your program. An example of a tiny program using the current time every second for a random number:
If you need time as an input anyway, and sample your other inputs based on this time, it's the easiest way. (If you already use
Time.fps
for this, useTime.timestamp
to get the actual time with it)2) At startup with a signal
If you don't normally need time as an input to your program, the previous solution is not ideal. You may prefer to initialise your program state with the start time of the program and not have to ignore a time ticker for the rest of the time the program runs.
It's probably easiest to do this with the signal-extra package*. Use
Signal.Time.startTime
to get a signal that doesn't tick but only has the start time of the program as the initial value. UseSignal.Extra.foldp'
so you can use the initial value of your inputs.*I may be biased because I'm the author of the linked package. But I don't know of other packages offering the same functionality.
3) At startup with a port
If you find the previous solution unsatisfactory, because you have this not-changing
Signal
to add to your input, this solution is for you. Here we use JavaScript interop to get the program startup time, and Elm will accept it as a constant value (noSignal
). The Elm code looks like so:So what's the downside here? You need to write some JavaScript. Instead of the standard
, you need something like this in your html file:
If you choose this way, perhaps it's a good idea to use a random number from JavaScript as your initial seed. I've read that that's more cryptographically secure (disclaimer: I don't know much about crypto). So you'd have a
port aRandomNumber : Int
and{aRandomNumber: Math.floor((Math.random() - 0.5) * 4294967295)}
.Just an update for people who got here from Google like I did: the recommended way to do this now is with
flags
instead ofports
. The code in the other answers will not even compile now.https://guide.elm-lang.org/interop/javascript.html
HTML
Elm
...