I have a program that displays a bunch of random triangles using Haskell, OpenGL, and GLUT. However I’d like the random triangles to change every time I click, or something of that nature (not important when they change).
Currently my working code looks like this:
main :: IO ()
main = do
(pname, _) <- getArgsAndInitialize
createWindow $ "Haskellisa"
-- TODO set based on command line args
windowSize $= (Size 640 480)
blend $= Enabled
blendFunc $= (SrcAlpha, OneMinusSrcAlpha)
displayCallback $= display
keyboardMouseCallback $= Just (keyboardMouse)
mainLoop
display :: IO ()
display = do
clear [ ColorBuffer ]
gen0 <- getStdGen
let (tris,gen1) = randomTris 10 gen0
let (cols,gen2) = randomColor4s 10 gen1
let triColPairs = zip tris cols
mapM_ (\(tri, col) -> drawTri tri col) triColPairs
flush
However display takes the same StdGen every time by calling getStdGen. What I’d like is for the gen2 from the line let (cols,gen2)... to be used as the generator for the next time, but obviously that requires some sort of mutable state or something of that nature.
What’s the best way to do what I’m asking, such that I will get different randomness every time display runs?
Since you’re in
IOanyway, you could just storegen2viasetStdGen:so your next round takes off where your previous stopped.
Another option is to
splittheStdGenand use one of the results usingnewStdGeninstead ofgetStdGen.