Haskell 中的随机数 [英] Random number in Haskell

查看:29
本文介绍了Haskell 中的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Haskell 中获取一个随机数.(我目前正在学习,还没有学习 Monads 或 IO 等)问题是 System.Random 中的函数都返回一个 IO Int,然后我不能在我使用的其余代码中使用整数和浮点数.

I'm trying to get a random number in Haskell. (Which I'm currently learning and haven't got on to Monads or IO, etc) the problem is the functions in System.Random all return an IO Int, which I then can't use in the rest of my code which uses Int and Float.

这里的目标是从列表中选择一对,其中第一对是代表概率的浮点数.所以我的计划是使用随机数根据概率选择一对.

The objective here is to choose a pair from a list where the first of the pair is a float representing a probability. So my plan was to use a random number to choose a pair based on its probability.

推荐答案

这是新 Haskell 程序员常见的障碍.您想要逃避 IO,需要一段时间才能找出最好的方法.Learn You A Haskell 教程很好地解释了使用 state monad 生成随机数的一种方法,但仍然必须使用 getStdGennewStdGen 进行播种,它们是在 IO 中.

This is a common barrier for new Haskell programmers. You want to escape IO, and it takes a while to figure out the best way to do so. The Learn You A Haskell tutorial has a good explanation of one way to generate random numbers using the state monad, but it still has to be seeded using getStdGen or newStdGen, which are in IO.

对于简单的情况,你可以做一些类似的事情

For simple cases you can just do something like

myPureFunction :: Float -> Float
myPureFunction x = 2 * x

main :: IO ()
main = do
    -- num :: Float
    num <- randomIO :: IO Float
    -- This "extracts" the float from IO Float and binds it to the name num
    print $ myPureFunction num

所以你看,你可以在 main 中获取你的随机数,然后将该值传递给一个执行处理的纯函数.

So you see, you can get your random number in main, then pass that value to a pure function that does the processing.

您可能会问自己,为什么要在 Haskell 中进行所有这些工作来生成随机数.有很多很好的理由,其中大部分都与类型系统有关.由于生成随机数需要修改操作系统中 StdGen 的状态,因此它必须存在于 IO 中,否则您可能会拥有一个纯函数,每次都会给您不同的结果.

You may be asking yourself why there's all this work to generate random numbers in Haskell. There are numerous good reasons, most of which have to do with the type system. Since generating random numbers requires modifying the state of the StdGen in the operating system, it has to live inside IO, otherwise you could have a pure function that gives you different results each time.

想象一下这个人为的场景:

Imagine this contrived scenario:

myConstant :: Int
myConstant = unsafePerformIO randomIO

blowUpTheWorld :: IO ()
blowUpTheWorld = error "Firing all the nukes"

main :: IO ()
main = do
    if even myConstant
        then print "myConstant is even"
        else blowUpTheWorld

如果你运行了几次,很可能你最终会发射所有的核武器".显然,这是不好的.myConstant 应该是常量,但是每次运行程序时都会得到不同的值.Haskell 希望保证在给定相同输入的情况下,纯函数将始终返回相同的值.

If you ran this a few times, chances are that you would end up "firing all the nukes". Obviously, this is bad. myConstant should be, well, constant, but each time you run the program you'd get a different value. Haskell wants to guarantee that a pure function will always return the same value given the same inputs.

它现在可能很烦人,但它是函数式程序员工具包中的一个强大工具.

It may be annoying right now, but it's a powerful tool in the functional programmer's kit.

这篇关于Haskell 中的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆