<div class="gmail_quote">2009/6/9 Krzysztof Skrzêtnicki <span dir="ltr"><<a href="mailto:gtener@gmail.com">gtener@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Tue, Jun 9, 2009 at 16:14, Daniel Fischer<<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>> wrote:<br>> If you're doing much with random generators, wrap it in a State monad.<br>
<br>
</div></div>To avoid reinventing the wheel one can use excellent package available<br>
on Hackage:<br>
<a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom" target="_blank">http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom</a></blockquote><div><br></div><div>Please do! Prefer MonadRandom to explicit generator passing: <a href="http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/">http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/</a>. Keep computations in MonadRandom, and pull them out with evalRandomIO at the last second.</div>
<div><br></div><div>Luke</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MonadRandom" target="_blank"></a><br>
<br>
> The die function simulates the roll of a die, picking a number between 1 and 6, inclusive, and returning it in the Rand monad.<br>
> Notice that this code will work with any source of random numbers g.<br>
><br>
> die :: (RandomGen g) => Rand g Int<br>
> die = getRandomR (1,6)<br>
><br>
> The dice function uses replicate and sequence to simulate the roll of n dice.<br>
><br>
> dice :: (RandomGen g) => Int -> Rand g [Int]<br>
> dice n = sequence (replicate n die)<br>
><br>
> To extract a value from the Rand monad, we can can use evalRandIO.<br>
><br>
> main = do<br>
> values <- evalRandIO (dice 2)<br>
> putStrLn (show values)<br>
<br>
Best regards<br>
<font color="#888888"><br>
Krzysztof Skrzêtnicki<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>