TurnsCoffeeIntoScripts
TurnsCoffeeIntoScripts

Reputation: 3918

Random number function

I'm trying to get a function that generate a random number, this is what I have so far:

getRandom :: Int -> Int -> Int
getRandom x y = do
    z <- randomRIO( x, y )

This code give me the following error:

The last statement in a 'do' block must be an expression

Now I understand this error, but I don't understand the solution.

I tried:

return z

But it doesn't work.

P.S. I'm very VERY new to Haskell

Upvotes: 3

Views: 161

Answers (1)

daniel gratzer
daniel gratzer

Reputation: 53901

A do block can't end in a binding. They desugar to something like

getRandom = randomRIO (x, y) >>= \z ->

which is obviously an error! If you want to just use the result of randomRIO

getRandom x y = randomRIO (x, y)

works fine. Additionally, if you just want a random number,

getRandom :: IO Integer
getRandom = randomIO

works.

Upvotes: 5

Related Questions