Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm seed for Random.initialSeed - prefer current time [duplicate]

Tags:

elm

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

like image 855
samspot Avatar asked Mar 09 '26 09:03

samspot


1 Answers

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
like image 160
pt2121 Avatar answered Mar 12 '26 06:03

pt2121