如何在Haskell中使用random-fu和平台无关的代码生成随机数?

时间:2012-09-14 00:03:08

标签: haskell random numbers multiplatform

我无法弄清楚如何使用Data.Random.Source.IO以多平台方式生成随机数。

我可以使用Data.Random.Source.DevRandom在Unix中生成随机数,并且在使用Data.Random.Source.MWC的Windows的GitHub文档中有一个示例,但是没有使用Data.Random.Source.IO的示例代码。 / p>

1 个答案:

答案 0 :(得分:4)

好的我已经将github示例转换为使用Source.IO

import Data.Random
import Data.Random.Source.IO


logNormal :: Double -> Double -> RVar Double
logNormal mu sigmaSq = do
    x <- normal mu sigmaSq
    return (exp x)

main = sample (logNormal 5 1) >>= print

您可以在Data.Random.Source.IO的{​​{3}}中看到它只是为MonadRandom IO定义了合适的实例。

您可以从列表中生成一个统一的随机数

import Data.Random
import Data.Random.Source.IO

main = sample (randomElement [0..9]) >>= print