Joao Albuquerque
Joao Albuquerque

Reputation: 466

Random IO typing in haskell

I want the function to draw a number, and if it is greater than 7, send an approval message and call this add function.

However, my function only falls on the 'else'. The "disapproved" message appears. I think it's the IO Float typing with Float. How can I solve this?

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!"

Upvotes: 0

Views: 1261

Answers (1)

Zeta
Zeta

Reputation: 105935

randomIO works like random, which uses [0,1) for fractional types.

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.

Use randomRIO instead, e.g.

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

Upvotes: 4

Related Questions