随机 IO 输入 haskell

Random IO typing in haskell

我要函数抽取一个数字,如果大于7,发送批准消息并调用此添加函数。

不过,我的功能只落在了'else'上。 "disapproved" 消息出现。我认为是IO Float typing with Float。我该如何解决?

mySort:: Float -> Int
mySort = ceiling(10 * x)


numberSort:: IO ()
numberSort = do    
    num <- randomIO :: IO Float
    print $ mySort num
    if(num >= 7) then
        do
            putStrLn ("approved!" ++ "\n") >> add
        else
        do
            putStrLn "disapproved!"

randomIO 的工作方式类似于 random,它使用 [0,1) 表示小数类型。

random :: RandomGen g => g -> (a, g)

The same as randomR, but using a default range determined by the type:

  • For bounded types (instances of Bounded, such as Char), the range is normally the whole type.
  • For fractional types, the range is normally the semi-closed interval [0,1). [emphasis mine]
  • For Integer, the range is (arbitrarily) the range of Int.

改用randomRIO,例如

num <- randomRIO (0, 10) :: IO Float