<div class="gmail_quote">2009/6/9 Krzysztof Skrzêtnicki <span dir="ltr">&lt;<a href="mailto:gtener@gmail.com">gtener@gmail.com</a>&gt;</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&lt;<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>&gt; wrote:<br>&gt; If you&#39;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>
&gt; 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>
&gt; Notice that this code will work with any source of random numbers g.<br>
&gt;<br>
&gt; die :: (RandomGen g) =&gt; Rand g Int<br>
&gt; die = getRandomR (1,6)<br>
&gt;<br>
&gt; The dice function uses replicate and sequence to simulate the roll of n dice.<br>
&gt;<br>
&gt; dice :: (RandomGen g) =&gt; Int -&gt; Rand g [Int]<br>
&gt; dice n = sequence (replicate n die)<br>
&gt;<br>
&gt; To extract a value from the Rand monad, we can can use evalRandIO.<br>
&gt;<br>
&gt; main = do<br>
&gt;   values &lt;- evalRandIO (dice 2)<br>
&gt;   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>