2

What's a simple way to do this?

The documentation for Random.initialSeed says:

"A good way to get an unexpected seed is to use the current time." 

http://package.elm-lang.org/packages/elm-lang/core/2.1.0/Random#initialSeed

After a ton of reading, I can only find "solutions" that are well beyond my understanding of Elm and Functional Programming. They also don't seem to be solutions to this problem.

I'm currently hardcoding:

Random.initialSeed 314

If you use a library, please include the name used to get it from elm package. I've seen a solution that says use Native.now but I can't figure out how to get that one.

stackoverflow is suggesting this one but I can't understand how to apply it to my usecase Elm Current Date

Community
  • 1
  • 1
samspot
  • 595
  • 1
  • 11
  • 24
  • 2
    Use a port to get the current time. See the answer here: http://stackoverflow.com/questions/35235708/elm-generate-random-number – robertjlooby Feb 13 '16 at 18:29
  • @robertjlooby Thanks! That ended up being the simplest way from what I've seen. The answer you linked to does a nice job of presenting the minimal code. I also didn't know how to create my own html before seeing that (mine was generated by elm-make). – samspot Feb 14 '16 at 20:15
  • Based on this thread, i marked my question as a duplicate as suggested. – samspot Feb 14 '16 at 20:17

2 Answers2

1

You can try case nelson's answer from How do I get the current time in Elm?

From elm repl:

> import Now
> import Random
> Now.loadTime |> round -- get current time in Int
1455406828183 : Int
> Now.loadTime |> round |> Random.initialSeed -- get the Seed
Seed { state = State 1560073230 678, next = <function>, split = <function>, range = <function> }
: Random.Seed

I also have the code on my repo here.

Note: don't forget "native-modules": true in elm-package.json.

Edit:

to try the code,

  1. git clone https://github.com/prt2121/elm-backup.git
  2. cd elm-backup/now
  3. elm make Now.elm
  4. add "native-modules": true in elm-package.json
  5. elm repl
Community
  • 1
  • 1
pt2121
  • 11,720
  • 8
  • 52
  • 69
  • What do I need to install to get Native.Now? elm says "I cannot find find module 'Native.Now'.". This is what blocked me when I tried the answer you linked to. I looked in your repo, but of course it doesn't tell me if you ran "elm package install X". I do have "native-modules": true -- does it matter where i put that in in elm-package.json? – samspot Feb 14 '16 at 03:12
  • @samspot The module is not publicly published. See my update. Thanks! – pt2121 Feb 14 '16 at 03:23
  • 1
    Thanks, i thought this was official - makes more sense now. – samspot Feb 14 '16 at 20:16
0

The simplest way I can think of is to use the Elm Architecture and Effects.tick mechanism to initialise the seed with a time value.

Here is an example of how this works:

import Html exposing (..) 
import Html.Events exposing (onClick) 
import Random exposing (Seed, generate, int, initialSeed)
import Time exposing (Time)
import Effects exposing (Effects, Never) 
import Task exposing (Task)
import StartApp 

type alias Model = { seed : Seed, value : Int} 

type Action = Init Time | Generate

init : (Model, Effects Action) 
init = (Model (initialSeed 42) 0, Effects.tick Init) 

modelFromSeed : Seed -> (Model, Effects Action) 
modelFromSeed seed =
  let 
    (value', seed') = generate (int 1 1000) seed
  in 
    (Model seed' value', Effects.none)

update : Action -> Model -> (Model, Effects Action) 
update action model =
  case action of 
    Init time -> 
      modelFromSeed (initialSeed (round time))
    Generate -> 
      modelFromSeed model.seed


view : Signal.Address Action -> Model -> Html 
view address model =
  div []
  [ text ("Current value:  " ++ (toString model.value))
  , br [] []
  , button [onClick address Generate] [text "New Value"]
  ]

app : StartApp.App Model   
app = StartApp.start 
  { init = init
  , update = update
  , view = view
  , inputs = []
  }


main : Signal Html
main = app.html

port tasks : Signal (Task Never ())
port tasks = app.tasks
pdamoc
  • 2,798
  • 15
  • 19