<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Hi!</div><div><br></div><div>Take a look at MonadRandom [1]. It is basically what you want without getting generator explicitly.</div><div><br></div><div>[1] <a href="https://hackage.haskell.org/package/MonadRandom">https://hackage.haskell.org/package/MonadRandom</a></div><br><div>
<div>With regards,</div><div>Nikita Kartashov</div><div><br></div><br class="Apple-interchange-newline">
</div>
<br><div><div>On 12 Feb 2016, at 04:14, Thomas Jakway <<a href="mailto:tjakway@nyu.edu">tjakway@nyu.edu</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">I'm having a bad time using the State monad to generate random numbers without carrying around a lot of StdGens manually.<br>I have this snippet in the IO monad:<br><br> ... IO stuff ...<br> gen <- getStdGen<br> let (numPlayers, numMatches) = (evalState genRandVariables gen) :: (Integer, Integer)<br> ... More IO stuff ...<br><br> where maxRandPlayers = 10 :: Integer<br> minRandMatches = 10 :: Integer<br> maxRandMatches = 100 :: Integer<br> genRandVariables = (do<br> np <- randomR (1, maxRandPlayers) --minimum 1 other player<br> nm <- randomR (minRandMatches, maxRandMatches)<br> return (np, nm)) :: State StdGen (Integer, Integer)<br><br><br>I get this error message:<br>test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:53:23:<br> Couldn't match expected type ‘StateT<br> StdGen Data.Functor.Identity.Identity Integer’<br> with actual type ‘g0 -> (Integer, g0)’<br> Probable cause: ‘randomR’ is applied to too few arguments<br> In a stmt of a 'do' block: np <- randomR (1, maxRandPlayers)<br> In the expression:<br> (do { np <- randomR (1, maxRandPlayers);<br> nm <- randomR (minRandMatches, maxRandMatches);<br> return (np, nm) }) ::<br> State StdGen (Integer, Integer)<br><br>test/Jakway/Blackjack/Tests/IntegrationTests/MatchTests.hs:54:23:<br> Couldn't match expected type ‘StateT<br> StdGen Data.Functor.Identity.Identity Integer’<br> with actual type ‘g1 -> (Integer, g1)’<br> Probable cause: ‘randomR’ is applied to too few arguments<br> In a stmt of a 'do' block:<br> nm <- randomR (minRandMatches, maxRandMatches)<br> In the expression:<br> (do { np <- randomR (1, maxRandPlayers);<br> nm <- randomR (minRandMatches, maxRandMatches);<br> return (np, nm) }) ::<br> State StdGen (Integer, Integer)<br><br>What's really baffling to me is I feel like this is how it *should* look--that the whole point of the state monad is to *not* have to explicitly pass the StdGen to randomR. What am I doing wrong?<br>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br></blockquote></div><br></body></html>