<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604</link>
    <title>MATLAB Central Newsreader - recovering the jth state of the random number generator</title>
    <description>Feed for thread: recovering the jth state of the random number generator</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 05 Sep 2008 23:57:01 -0400</pubDate>
      <title>recovering the jth state of the random number generator</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604#598966</link>
      <author>Tudor </author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
i am trying to produce a stimulus sequence using the following procedure:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;rand ('state', 0)&lt;br&gt;
&lt;br&gt;
for i = 1:90000&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;randomnoisematrix = rand (45,45)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Once this stimulus is presented, i have to reverse correlate the response to various epochs of the stimulus.  Say, to the states of the stimulus between the 43000 and 43060 iterations.&lt;br&gt;
&lt;br&gt;
I find that if i run the iteration above repeatedly, to any given point ( say 43001) i get back the same randomnoise matrix as expected, however if i   ask for &lt;br&gt;
rand ('state', 43001)&lt;br&gt;
randmonoisematrix = rand (45, 45) i get a completely different output.  &lt;br&gt;
I other words, it appears that the state 0 is not followed by the state 1, 2, 3 etc.&lt;br&gt;
Does anybody know what is going on ?  &lt;br&gt;
&lt;br&gt;
The reason i want to do this is because i do not want to have to run each time through the whole sequence to get to my epoch of interest, but rather would like to &quot;skip&quot; ahead to the state i want (inbetween 43000 and 43060 for example).&lt;br&gt;
&lt;br&gt;
Thanks a lot.&lt;br&gt;
&lt;br&gt;
Tudor</description>
    </item>
    <item>
      <pubDate>Sat, 06 Sep 2008 01:00:28 -0400</pubDate>
      <title>Re: recovering the jth state of the random number generator</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604#598973</link>
      <author>Walter Roberson</author>
      <description>Tudor wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I find that if i run the iteration above repeatedly, to any given point &lt;br&gt;
&amp;gt; ( say 43001) i get back the same randomnoise matrix as expected, however if i   ask for &lt;br&gt;
&amp;gt; rand ('state', 43001)&lt;br&gt;
&amp;gt; randmonoisematrix = rand (45, 45) i get a completely different output.  &lt;br&gt;
&amp;gt; I other words, it appears that the state 0 is not followed by the state 1, 2, 3 etc.&lt;br&gt;
&lt;br&gt;
Correct. The state is used to initialize the internal binary data used in&lt;br&gt;
the pseudo-random number generator. The state is not a counter of how many&lt;br&gt;
values have been produced.&lt;br&gt;
&lt;br&gt;
&amp;gt; The reason i want to do this is because i do not want to have to run each time&lt;br&gt;
&amp;gt; through the whole sequence to get to my epoch of interest, but rather would like&lt;br&gt;
&amp;gt; to &quot;skip&quot; ahead to the state i want (inbetween 43000 and 43060 for example).&lt;br&gt;
&lt;br&gt;
You will have to create your own random number generator then. For example, you&lt;br&gt;
could MD5 the state number, extract some bits from the MD5 and typecast() the&lt;br&gt;
result to a floating point number.&lt;br&gt;
&lt;br&gt;
Or you could research one of the linear congruential pseudo random number generators.&lt;br&gt;
Although they are usually processed sequentially, if you go back to the theory&lt;br&gt;
of them, you could go directly to any particular state without a lot more work&lt;br&gt;
than raising a large number to a power (the state number) modulo a large number.&lt;br&gt;
You would have to use indefinite precision arithmetic to do it the naive way as&lt;br&gt;
the values get *huge*, but fortunately you can take advantage of the fact that&lt;br&gt;
the number and the modulus will be relatively prime (for any PRNG of maximum&lt;br&gt;
length) and there are theorems that in that case allow the work to be done to&lt;br&gt;
be reduced enormously (to a practical level, if you have the right software.)&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Q = quotation(rand);&lt;br&gt;
if isempty(Q); error('Quotation server filesystem problems')&lt;br&gt;
else sprintf('%s',Q), end</description>
    </item>
    <item>
      <pubDate>Sat, 06 Sep 2008 19:59:02 -0400</pubDate>
      <title>Re: recovering the jth state of the random number generator</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604#599057</link>
      <author>Tudor </author>
      <description>Thanks a lot for your explanation.  That was a crucial thing to understand.  I am a biologist trying to design some physiology experiments, so my math is far from developed. &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
How about if i 'seed' the rand function at each iteration ?:&lt;br&gt;
&lt;br&gt;
for i = 1:90000&lt;br&gt;
&lt;br&gt;
rand ('state', i);&lt;br&gt;
randmonoisematrix = rand (45, 45);&lt;br&gt;
&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Intuitively, this the same as sampling a random space of values, and perchance the subsample would be random as well).  The goal for me is not necessarily to alter the stimulus on each presentation, but rather to ensure that the &quot;surface&quot; of the matrix is as &quot;diverse&quot; as possible, in the x,y dimension, as well as in the temporal one, for the duration of the stimulus.   I imagine this would not work if there is some periodicity of the sequence that can add up to the integer value. &lt;br&gt;
&amp;nbsp;Thanks again for your insight.  Is there a refference that describes the math behind the rand and randn functions ?&lt;br&gt;
&lt;br&gt;
Tudor&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I find that if i run the iteration above repeatedly, to any given point &lt;br&gt;
&amp;gt; &amp;gt; ( say 43001) i get back the same randomnoise matrix as expected, however if i   ask for &lt;br&gt;
&amp;gt; &amp;gt; rand ('state', 43001)&lt;br&gt;
&amp;gt; &amp;gt; randmonoisematrix = rand (45, 45) i get a completely different output.  &lt;br&gt;
&amp;gt; &amp;gt; I other words, it appears that the state 0 is not followed by the state 1, 2, 3 etc.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Correct. The state is used to initialize the internal binary data used in&lt;br&gt;
&amp;gt; the pseudo-random number generator. The state is not a counter of how many&lt;br&gt;
&amp;gt; values have been produced.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The reason i want to do this is because i do not want to have to run each time&lt;br&gt;
&amp;gt; &amp;gt; through the whole sequence to get to my epoch of interest, but rather would like&lt;br&gt;
&amp;gt; &amp;gt; to &quot;skip&quot; ahead to the state i want (inbetween 43000 and 43060 for example).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; You will have to create your own random number generator then. For example, you&lt;br&gt;
&amp;gt; could MD5 the state number, extract some bits from the MD5 and typecast() the&lt;br&gt;
&amp;gt; result to a floating point number.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Or you could research one of the linear congruential pseudo random number generators.&lt;br&gt;
&amp;gt; Although they are usually processed sequentially, if you go back to the theory&lt;br&gt;
&amp;gt; of them, you could go directly to any particular state without a lot more work&lt;br&gt;
&amp;gt; than raising a large number to a power (the state number) modulo a large number.&lt;br&gt;
&amp;gt; You would have to use indefinite precision arithmetic to do it the naive way as&lt;br&gt;
&amp;gt; the values get *huge*, but fortunately you can take advantage of the fact that&lt;br&gt;
&amp;gt; the number and the modulus will be relatively prime (for any PRNG of maximum&lt;br&gt;
&amp;gt; length) and there are theorems that in that case allow the work to be done to&lt;br&gt;
&amp;gt; be reduced enormously (to a practical level, if you have the right software.)</description>
    </item>
    <item>
      <pubDate>Tue, 09 Sep 2008 14:30:40 -0400</pubDate>
      <title>Re: recovering the jth state of the random number generator</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604#599470</link>
      <author>Peter Perkins</author>
      <description>Tudor wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; How about if i 'seed' the rand function at each iteration ?:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 1:90000&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rand ('state', i);&lt;br&gt;
&amp;gt; randmonoisematrix = rand (45, 45);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
Tudor, I recommend not doing that.  Some people try to do that each time they generate a single value, and that's definitely a bad idea, because the statistical properties of the generator are completely destroyed.  Reseeding each time you generate a 45x45 matrix isn't quite as bad, but still ...&lt;br&gt;
&lt;br&gt;
What you're looking for is often called substreams.&lt;br&gt;
&lt;br&gt;
If you have access to the MATLAB prerelease, I recommend looking at that.</description>
    </item>
    <item>
      <pubDate>Tue, 09 Sep 2008 16:14:02 -0400</pubDate>
      <title>Re: recovering the jth state of the random number generator</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235604#599489</link>
      <author>Tudor </author>
      <description>Hi Peter,&lt;br&gt;
&lt;br&gt;
i studied a little into it, and begin to understand how the random number generator actually works.  Since it is not simply a f(x), with x being the successive states, one cannot recover the jth state after a given beginning seed except by running the actual generator with the correct seed.  Since my application is to produce a succesion of matrices (which will become images after a lookup table application) that are random with respect to each other, but  easy to recover to be able to run reverse correlations of stimulus responses on the given stimulus&lt;br&gt;
&amp;nbsp;&amp;nbsp;rand('state', myseed);&lt;br&gt;
&amp;nbsp;&amp;nbsp;for i  = 1:framenumber&lt;br&gt;
&amp;nbsp;&amp;nbsp;noisematrix = rand (side,side)&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
i have take the typical approach of the molecular biologist.&lt;br&gt;
I ran the above loop offline, and stored all the frames.  Since i need a grayscale lookup table (0-255), that fits in a uint8, the whole thing is under 200 Mb.  Then i can reverse correlate at ease, on the saved file.&lt;br&gt;
&lt;br&gt;
I have to say everybody that answered to my quest was very helpful, and i will be back for sure with more simpleminded questions.&lt;br&gt;
&lt;br&gt;
Tudor&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;ga61ag$1lt$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Tudor wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; How about if i 'seed' the rand function at each iteration ?:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; for i = 1:90000&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; rand ('state', i);&lt;br&gt;
&amp;gt; &amp;gt; randmonoisematrix = rand (45, 45);&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Tudor, I recommend not doing that.  Some people try to do that each time they generate a single value, and that's definitely a bad idea, because the statistical properties of the generator are completely destroyed.  Reseeding each time you generate a 45x45 matrix isn't quite as bad, but still ...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What you're looking for is often called substreams.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If you have access to the MATLAB prerelease, I recommend looking at that.</description>
    </item>
  </channel>
</rss>

