Well, I don't know how many generators you need. But I'm sure you can pass them around in a way that doesn't leak.<br><br><div><span class="gmail_quote">On 7/31/07, <b class="gmail_sendername">Chad Scherrer</b>
<<a href="mailto:chad.scherrer@gmail.com">chad.scherrer@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Ok, that looks good, but what if I need some random values elsewhere
<br>in the program? This doesn't return a new generator (and it can't<br>because you never get to the end of the list). Without using IO or ST,<br>you'd have to thread the parameter by hand or use the State monad,
<br>right? This is where I was leaking space before.<br><br>Actually, this makes me wonder... I think what killed it before was<br>that the state was threaded lazily through the various (= very many)<br>calls. I suppose a State' monad, strict in the state, could help here.
<br>I wonder how performance for this would compare with IO or ST. Might<br>have to try that sometime...<br><br>Chad<br><br>On 7/31/07, Lennart Augustsson <<a href="mailto:lennart@augustsson.net">lennart@augustsson.net
</a>> wrote:<br>> No leak in sight.<br>><br>> -- Lennart<br>><br>> import Random<br>> import Array<br>><br>> randomElts :: RandomGen g => g -> [a] -> [a]<br>> randomElts _ [] = []<br>
> randomElts g xs = map (a!) rs<br>> where a = listArray (1, n) xs<br>> rs = randomRs (1, n) g<br>> n = length xs<br>><br>> main = do<br>> g <- getStdGen<br>> let xs = randomElts g [10,2,42::Int]
<br>> print $ sum $ take 1000000 xs<br>_______________________________________________<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">
http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>