<div dir="ltr">You are likely missing a `lift` when you call the random functions.<div><br></div><div>Here is an example:</div><div><div><br></div><div><div><br></div><div><font face="courier new, monospace">import Control.Monad.State</font></div><div><font face="courier new, monospace">import Control.Monad.Random</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">walk :: RandomGen g => StateT (Float, Float) (Rand g) (Float, Float)</font></div><div><font face="courier new, monospace">walk = do (x, y) <- get</font></div><div><font face="courier new, monospace">          put (x + 1, y + 1)</font></div><div><font face="courier new, monospace">          get >>= return</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">foo :: RandomGen g => StateT (Float,Float) (Rand g) ()</font></div><div><font face="courier new, monospace">foo = do</font></div><div><font face="courier new, monospace">  a <- lift $ getRandomR (1,6)</font></div><div><font face="courier new, monospace">  b <- lift $ getRandomR (4,10)</font></div><div><font face="courier new, monospace">  (x,y) <- get</font></div><div><font face="courier new, monospace">  put (x+a, y+b)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">test1 = do</font></div><div><font face="courier new, monospace">  g <- getStdGen</font></div><div><font face="courier new, monospace">  print $ runRand (runStateT foo (0.0, 3.14)) g</font></div><div><br></div></div>Because the State monad is encapsulating (transforming) the random monad, you have to `lift` operations in the random monad so that they become operations in the transformed monad.</div><div><br>On Friday, June 17, 2016 at 11:22:57 PM UTC-5, Christopher Howard wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Hi. I'm working through "Haskell Design Patterns" and got inspired to<br>try to create my first monad stack. What I really wanted though (not<br>shown in the book) was to combine State and Rand. I daresay I got<br>something to compile:<p>walk :: RandomGen g => StateT (Float, Float) (Rand g) (Float, Float)<br>walk = do (x, y) <- get<br>          put (x + 1, y + 1)<br>          get >>= return</p><p>However, the moment I try to insert a getRandomR or something in it, I<br>get an error</p><p>Could not deduce (MonadRandom (StateT (Float, Float) (Rand g)))<br>      arising from a use of `getRandomR' <...snip...><br>add an instance declaration for<br>      (MonadRandom (StateT (Float, Float) (Rand g)))</p><p>I see there are instances</p><p>MonadRandom m => MonadRandom (StateT s m)<br>RandomGen g => MonadRandom (Rand g)</p><p>in Control.Monad.Random.Class, so I am not quite sure what is expected<br>of me.</p><p></p><p>-- <br><a href="http://justonemoremathproblem.com" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fjustonemoremathproblem.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHsqhxVHmezqVeYzSLFRodpyfjN6g';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fjustonemoremathproblem.com\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHsqhxVHmezqVeYzSLFRodpyfjN6g';return true;">http://justonemoremathproblem.<wbr>com</a><br>To protect my privacy, please use PGP encryption. It's free and easy<br>to use! My public key ID is 0x340EA95A (<a href="http://pgp.mit.edu" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpgp.mit.edu\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH6oRF1fztIUOrGL6QkLtpnxyDNJA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpgp.mit.edu\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH6oRF1fztIUOrGL6QkLtpnxyDNJA';return true;">pgp.mit.edu</a>).</p><p>______________________________<wbr>_________________<br>Haskell-Cafe mailing list<br><a href="javascript:" target="_blank" gdf-obfuscated-mailto="zZyF1OjpAQAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Haskel...@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fhaskell-cafe\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH7sFgl7KfuDcDlaGGG3ip3kRaoIA';return true;">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p></blockquote></div></div>