<HTML>
<HEAD>
<TITLE>State Monad - using the updated state in an adhoc manner</TITLE>
</HEAD>
<BODY>
<FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Hi,<BR>
<BR>
I&#8217;m a newbie looking to get my head around using the State Monad for random number generation. &nbsp;I&#8217;ve written non-monad code that achieves this no problem. &nbsp;When attempting to use the state monad I can get what I know to be the correct initial value and state, but can&#8217;t figure out for the life of me how to then increment it without binding more calls there and then. &nbsp;Doing several contiguous calls is not what I want to do here &#8211; and the examples I&#8217;ve read all show this (using something like liftM2 (,) myRandom myRandom). &nbsp;I want to be able to do:<BR>
<BR>
Get_a_random_number<BR>
<BR>
&lt; a whole load of other stuff &gt;<BR>
<BR>
Get the next number as defined by the updated state in the first call<BR>
<BR>
&lt;some more stuff&gt;<BR>
<BR>
Get another number, and so on.<BR>
<BR>
I get the first number fine, but am lost at how to get the second, third, forth etc without binding there and then. &nbsp;I just want each number one at a time where and when I want it, rather than saying give 1,2,10 or even &#8216;n&#8217; numbers now. &nbsp;I&#8217;m sure it&#8217;s blindly obvious!<BR>
<BR>
Note: I&#8217;m not using Haskell&#8217;s built in Random functionality (nor is that an option), I&#8217;ll spare the details of the method I&#8217;m using (NRC&#8217;s ranq1) as I know it works for the non-Monad case, and it&#8217;s irrelevent to the question. &nbsp;So the code is:<BR>
<BR>
ranq1 :: Word64 -&gt; ( Double, Word64 )<BR>
ranq1 state = ( output, newState )<BR>
&nbsp;&nbsp;where<BR>
&nbsp;&nbsp;&nbsp;&nbsp;newState = ranq1Increment state<BR>
&nbsp;&nbsp;&nbsp;&nbsp;output = convert_to_double newState<BR>
<BR>
ranq1Init :: Word64 -&gt; Word64<BR>
ranq1Init = convert_to_word64 . ranq1Increment . xor_v_init <BR>
<BR>
-- I&#8217;ll leave the detail of how ranq1Increment works out for brevity. &nbsp;I know this bit works fine. &nbsp;Same goes for the init function it&#8217;s just providing an initial state.<BR>
<BR>
-- The Monad State Attempt<BR>
getRanq1 :: State Word64 Double<BR>
getRanq1 = do<BR>
&nbsp;&nbsp;state &lt;- get<BR>
&nbsp;&nbsp;let ( randDouble, newState ) = ranq1 state<BR>
&nbsp;&nbsp;put newState<BR>
&nbsp;&nbsp;return randDouble<BR>
<BR>
<BR>
_________ And then in my main _________<BR>
<BR>
-- 124353542542 is just an arbitrary seed<BR>
main :: IO()<BR>
main = do<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let x = evalState getRanq1 (ranq1Init 124353542542)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print (x)<BR>
<BR>
<BR>
As I said this works fine; x gives me the correct first value for this sequence, but how do I then get the second and third without writing the giveMeTenRandoms style function? &nbsp;I guess what I want is a next() type function, imperatively speaking.<BR>
<BR>
<BR>
Many thanks for any help,<BR>
<BR>
<BR>
Phil.<BR>
<BR>
</SPAN></FONT></FONT>
</BODY>
</HTML>