As I wrote this question I found a solution that worked in the Happstack crash course (templates).
In your route which has a return type of ServerPart Response
you can use the liftIO
monad transformer to be able to perform IO actions. There's this handy function called randomRIO
with generates a random Int
from an input of a tuple which two Int
s as range, like this:
page :: ServerPart Response
page = do
randID <- liftIO $ randomRIO (0, max)
... Code to generate response ...
where max = length questions
randomRIO
can be found in System.Random
and liftIO
can be found in Control.Monad.Trans
.