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

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

问题描述

我试图在Haskell中获得一个随机数。 (我目前正在学习,还没有上Monads或IO等)问题是System.Random中的函数都返回一个IO Int,然后我不能在我的代码的其余部分使用它Int和Float。

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

解决方案

这是新Haskell程序员的共同障碍。您希望转义IO,并且需要一段时间才能找出最佳方式。学习你的Haskell教程对使用状态monad生成随机数的一种方法有很好的解释,但它仍然必须使用 getStdGen newStdGen ,它们在IO中。



对于简单的情况,您可以执行一些操作,比如

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

main :: IO()
main = do
- num :: Float
num < - randomIO :: IO Float
- 从IO Float中抽取浮点数并将其绑定到名称num
print $ myPureFunction num

所以你看,你可以在 main 中得到你的随机数,然后将该值传递给一个纯函数,处理。

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

想象一下这个假设:

  myConstant :: Int 
myConstant = unsafePerformIO randomIO

blowUpTheWorld :: IO()
blowUpTheWorld =错误烧制所有核武器

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

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

现在可能很烦人,但它是功能程序员的强大工具套件。

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.

解决方案

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

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

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

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天全站免登陆