The types IO Int
and Int
are entirely different in Haskell. This applies to any other type of that form, like Maybe Int
or Either String Int
. This is part of Haskell's type system design that makes it so powerful. You can think of anything in this form as a sort of container, it's parametrized over that type. Therefore you can do something like
getRandom :: IO Int
getRandom = do
seed <- newIORef 7 -- IO (IORef Int)
g <- readIORef seed -- IO Int
let (x, newG) = randomGen g -- (Int, Int)
writeIORef seed newG -- IO ()
return x -- IO Int
However, this will always return the same value since the seed is discarded after every call. I'm curious as to why you want to take this approach to generating random numbers at all, since there is such a nice API in the MonadRandom package. See this answer I wrote a while back for an example of how to use the Rand
monad, and this answer for a bit more of an in depth explanation of how it works.