<p dir="ltr">It occurs to me that this would be best done by a specific method, like:</p>
<p dir="ltr">interleaveRandom :: (Monad m, RandomGen g) => StateT g m a -> StateT g m a<br>
interleaveRandom (StateT m) = StateT $ \g -> let (gl, gr) = split g in liftM (\p -> (fst p, gr)) $ m gl</p>
<p dir="ltr">It'd act like unsafeInterleaveIO and unsafeInterleaveST, but it'd be safe, and you would know when it actually was splitting.</p>
<div class="gmail_quote">On Jul 30, 2015 1:15 PM, "Roman Cheplyaka" <<a href="mailto:roma@ro-che.info">roma@ro-che.info</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 30/07/15 20:38, Zemyla wrote:<br>
> Normally, a monad transformer to provide a random number generator would<br>
> be of the form StateT g, where g is a RandomGen. But I've seen some<br>
> libraries (like QuickCheck) define their RandomT as:<br>
><br>
> newtype RandomT g m a = RandomT { runRandomT :: g -> m a }<br>
><br>
> with their monadic bind operation defined as<br>
><br>
> (RandomT m) >>= f = RandomT $ \g -> let (ga, gb) = split g in m ga >>=<br>
> (\a -> runRandomT (f a) gb)<br>
><br>
> and return and fail as in ReaderT.<br>
><br>
> Can someone describe the advantages and disadvantages of doing RandomT<br>
> this way? I mean, if your generator has a subpar split operation (and<br>
> most do), this will obviously exacerbate any problems with it.<br>
<br>
tf-random addresses this.<br>
<br>
> Does it give any comparable advantages?<br>
<br>
It doesn't introduce data dependencies. Let's say you generate a random<br>
binary tree. With the split approach, you can take the right subtree<br>
without evaluating the left one.<br>
<br>
Roman<br>
<br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div>