我只想看一个随机数。所以这里是 an example straight out of the docs对于随机库。我希望 Random.generate
接受生成器和种子并返回包含随机值和新种子的元组,如:
generate : Generator a -> Seed -> (a, Seed)
-- Main.elm
import Random
seed0 = Random.initialSeed 31415
randomNumber = Random.generate (Random.int 0 10) seed0
main =
-- print result of randomNumber here
编译器错误显示两种类型不匹配:
-- TYPE MISMATCH ---------------------------------------------------- -----------
The 2nd argument to function `generate` is causing a mismatch.
5| Random.generate (Random.int 0 10) seed0
^^^^^
Function `generate` is expecting the 2nd argument to be:
Random.Generator a
But it is:
Random.Seed
The 1st argument to function `generate` is causing a mismatch.
5| Random.generate (Random.int 0 10) seed0
^^^^^^^^^^^^^^^
Function `generate` is expecting the 1st argument to be:
a -> b
But it is:
Random.Generator Int
我在这里错过了什么?
最佳答案
您引用的文档版本是Core 1.0.0,它是旧的。当前版本的核心是 4.0.5。 (docs for Random here)
您要查找的带有签名的函数现在命名为 step
:
step : Generator a -> Seed -> (a, Seed)
所以你重构的代码看起来像这样:
import Html exposing (text)
import Random
seed0 = Random.initialSeed 31415
(randomNumber, nextSeed) = Random.step (Random.int 0 10) seed0
main =
text <| toString randomNumber
关于random - 在 Elm 中生成随机数所需的最少代码是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206791/