What is the expected behavior of Random.initialSeed in Elm (v0.15)
Is initialSeed supposed to provide a different seed every time it is called? The following code always gives the same value:
import Graphics.Element exposing (show)
import Random exposing(float, generate, initialSeed)
main =
show (generate (float 0 1 ) (initialSeed 31415))
If this code behaves correctly, could you please provide a pointer to the use of random numbers and Random.generate.
source to share
initialSeed
is a function that Int
creates a Seed
, which can then be used in a function generate
. Since Elm is purely a function, giving is generate
the same Generator
and Seed
will always give the same value. However, it generate
also returns the next one Seed
, which will be used on the next call generate
. This allows you to reproduce the same sequence of randomly generated values.
Usage example:
import Random
import Graphics.Element as Element
-- Int Generator that computes random numbers in the range [0, 10]
intGenerator = Random.int 0 10
-- Change this to get different sequences.
seed = Random.initialSeed 42
--seed = Random.initialSeed 1234
--seed = Random.initialSeed 0
-- Generates a random x and returns the next seed to be used. Note:
-- This will always return the same x because it uses the same seed
-- this is a result of Elm being purely functional. However,
-- because this returns a new seed, you can use that to get the next
-- random value in the sequence
(x, seed') = (Random.generate intGenerator seed)
-- Generate the next element in the sequence
(y, seed'') = (Random.generate intGenerator seed')
(z, seed''') = (Random.generate intGenerator seed'')
main = Element.show [x, y, z]
at share-elm.com: http://share-elm.com/sprout/55c766b4e4b06aacf0e8be1b
Hope this helps!
source to share
Usage Random
is super-easy in Elm 0.15. (I assume you understand how Elm naturally divides data and code into model, update, and view. If your understanding of Elm architecture is unstable, read The Elm Architecture tutorial and a simple explanation of the model-model update pattern .)
The most important function generate
, it returns a pair: a random value and a new seed. This new seed can be transferred to again generate
to get a new random value and another seed. However, passing the same seed gives the same "random" meaning. The code below displays 59 :
import Graphics.Element exposing (show)
import Random exposing (generate, int, initialSeed)
main =
let (number, seed) = generate (int 0 99) (initialSeed 1234)
in show number
You need to keep this new seed between any 2 random generations of values. And every time you use generate
, transfer a new seed instead of the old one. How do you do it?
You keep the seed inside your model and make sure that during the update step you overwrite the old seed with the new seed:
import Graphics.Element exposing (Element, show)
import Mouse exposing (clicks)
import Random exposing (Seed, initialSeed, generate, int)
import Signal exposing (Signal, foldp)
seed0 = initialSeed 1234
type alias Model = { num : Int, seed : Seed }
defaultModel : Model
defaultModel = { num = 0, seed = seed0 } -- initial seed in the model
update : () -> Model -> Model
update _ model =
let (newNum, newSeed) = generate (int 0 99) model.seed -- new value and new seed
in { model | num <- newNum, seed <- newSeed } -- update model with new seed
view : Model -> Element
view model = show model.num
-- new random value each time mouse clicked
main = Signal.map view <| foldp update defaultModel clicks
source to share