Elm seed for Random.initialSeed-首选当前时间

Elm seed for Random.initialSeed-首选当前时间,elm,Elm,做这件事的简单方法是什么 Random.initialSeed的文档说明: "A good way to get an unexpected seed is to use the current time." 经过大量阅读,我只能找到远远超出我对Elm和函数式编程理解的“解决方案”。它们似乎也不是这个问题的解决方案 我目前正在硬编码: Random.initialSeed 314 如果您使用库,请包括用于从elm软件包获取库的名称。我看到了一个解决方案,它说使用本机。现在,但我不知道如何

做这件事的简单方法是什么

Random.initialSeed的文档说明:

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

经过大量阅读,我只能找到远远超出我对Elm和函数式编程理解的“解决方案”。它们似乎也不是这个问题的解决方案

我目前正在硬编码:

Random.initialSeed 314
如果您使用库,请包括用于从elm软件包获取库的名称。我看到了一个解决方案,它说使用
本机。现在
,但我不知道如何获得该解决方案


stackoverflow建议使用此选项,但我不明白如何将其应用到我的用例中

您可以从

来自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
>立即导入
>随机输入
>Now.loadTime |>round——以整数表示当前时间
1455406828183:Int
>Now.loadTime |>round |>Random.initialSeed--获取种子
种子{state=state 1560073230 678,next=,split=,range=}
:随机。种子
我的回购协议上也有代码。

注意:不要忘记
elm package.json
中的
“本机模块”:true

编辑:

要尝试代码

  • git克隆
  • cd elm备份/now
  • elm现在就做,elm
  • elm package.json中添加
    “本机模块”:true
  • 榆树

  • 我能想到的最简单的方法是使用Elm架构和
    Effects.tick
    机制以时间值初始化种子

    下面是一个如何工作的示例:

    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
    

    使用端口获取当前时间。在这里看到答案:@robertjlooby谢谢!这是我见过的最简单的方法。您链接到的答案很好地展示了最少的代码。在看到之前,我也不知道如何创建自己的html(我的html是由elm make生成的)。基于此线程,我按照建议将我的问题标记为重复。我需要安装什么才能获得本机。现在?elm说“我找不到模块'Native.Now'。”。这就是当我尝试你链接的答案时阻止我的原因。我查看了你的回购协议,但它当然没有告诉我你是否运行了“elm package install X”。我确实有“本机模块”:是的——我把它放在elm package.json中的什么地方有关系吗?@samspot该模块没有公开发布。查看我的更新。谢谢谢谢,我认为这是官方的-现在更有意义了。