I have a simple little Happstack application that shows a form with an email field and a random question field to help combat spam. To get a random number I use getStdGen
in my main
function and pass it along to my function which creates the html. The problem is that the same StdGen
is used so my random value is not random unless I restart the application.
Here's what my Main.hs
looks like:
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
module Main where
import Happstack.Lite
import qualified Pages.Contact as Contact
import System.Random
main :: IO ()
main = do
gen <- getStdGen
serve Nothing $ pages gen
pages :: StdGen -> ServerPart Response
pages g = msum
[ dir "contact" $ Contact.page g
... Other irrelevant pages
]
And here's the function that uses the StdGen
to retrive a random question id:
getRandomQID :: StdGen -> Int
getRandomQID g =
let (rpercent, _) = random g :: (Float, StdGen)
rid = rpercent * questionsAmount
in round rid
questionsAmount :: (Num a) => a
questionsAmount = (fromIntegral . length) questions
What is the most elegant way to solve this problem?