<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: New Ways With Random Numbers, Part I</title>
	<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Sun, 08 Nov 2009 02:14:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Joe Pohedra</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30073</link>
		<dc:creator>Joe Pohedra</dc:creator>
		<pubDate>Tue, 03 Mar 2009 21:32:06 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30073</guid>
		<description>Thanks Peter,
(I submitted what became comment #18 before I updated the page and saw your reply #17.) By way of further explanation, the reason I am re-producing the distributions is to add to or extend models that they feed, without changing the partial results of the previous simulation. This happens in different Matlab sessions, and/or with intervening independent runs in other directories. I'll try a few more experiments to better understand when or if restoring the seed is necessary.</description>
		<content:encoded><![CDATA[<p>Thanks Peter,<br />
(I submitted what became comment #18 before I updated the page and saw your reply #17.) By way of further explanation, the reason I am re-producing the distributions is to add to or extend models that they feed, without changing the partial results of the previous simulation. This happens in different Matlab sessions, and/or with intervening independent runs in other directories. I&#8217;ll try a few more experiments to better understand when or if restoring the seed is necessary.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30069</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Tue, 03 Mar 2009 20:02:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30069</guid>
		<description>Joe, that does solve your problem, but worrying about the seed if you've saved the state is entirely unnecessary.  You should look into my previous comments -- your problem, I believe, was that the default stream changed out from under you, and the second time through you were setting the state of a stream that RAND was no longer using.  So yes, creating and getting a new default one solves the problem, but understanding why you were having a problem might be more instructive.</description>
		<content:encoded><![CDATA[<p>Joe, that does solve your problem, but worrying about the seed if you&#8217;ve saved the state is entirely unnecessary.  You should look into my previous comments &#8212; your problem, I believe, was that the default stream changed out from under you, and the second time through you were setting the state of a stream that RAND was no longer using.  So yes, creating and getting a new default one solves the problem, but understanding why you were having a problem might be more instructive.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Pohedra</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30068</link>
		<dc:creator>Joe Pohedra</dc:creator>
		<pubDate>Tue, 03 Mar 2009 19:38:18 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30068</guid>
		<description>I believe I found the answer to my question about repeating the random sequence used to generate distributions. In addition to saving and restoring the State, I should also save and restore the Seed that I use to initialize the random stream.

For the initial distributions, initialize the random stream:
savedSeed = sum(100*clock);
RandStream.setDefaultStream(RandStream('mt19937ar','seed',savedSeed));
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save('RandInit', 'savedSeed', 'savedState');

To re-generate the same distributions again later:
load RandInit.mat
RandStream.setDefaultStream(RandStream('mt19937ar','seed',savedSeed));
defaultStream = RandStream.getDefaultStream;
defaultStream.State = savedState;

I don't think I could have worked this out without the benefit of the descriptions and examples shown in the two parts of "New Ways With Random Numbers".
Thanks,
Joe</description>
		<content:encoded><![CDATA[<p>I believe I found the answer to my question about repeating the random sequence used to generate distributions. In addition to saving and restoring the State, I should also save and restore the Seed that I use to initialize the random stream.</p>
<p>For the initial distributions, initialize the random stream:<br />
savedSeed = sum(100*clock);<br />
RandStream.setDefaultStream(RandStream(&#8217;mt19937ar&#8217;,&#8217;seed&#8217;,savedSeed));<br />
defaultStream = RandStream.getDefaultStream;<br />
savedState = defaultStream.State;<br />
save(&#8217;RandInit&#8217;, &#8217;savedSeed&#8217;, &#8217;savedState&#8217;);</p>
<p>To re-generate the same distributions again later:<br />
load RandInit.mat<br />
RandStream.setDefaultStream(RandStream(&#8217;mt19937ar&#8217;,&#8217;seed&#8217;,savedSeed));<br />
defaultStream = RandStream.getDefaultStream;<br />
defaultStream.State = savedState;</p>
<p>I don&#8217;t think I could have worked this out without the benefit of the descriptions and examples shown in the two parts of &#8220;New Ways With Random Numbers&#8221;.<br />
Thanks,<br />
Joe</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30067</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Tue, 03 Mar 2009 18:14:15 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30067</guid>
		<description>Joe, when I run that code I get what I expect - the first W is the same as the second.  You haven't said what you're doing between these two code snippets, but if you're not getting what I get, then presumably that's where to look.  Here's what I suspect happened:

You created defaultStream by calling getDefaultStream.  But even though you named it "defaultStream" doesn't mean that someone can't change the default stream out from under you.  Your defaultStream will remain a valid stream, but not necessarily the &lt;b&gt;default&lt;/b&gt; stream.  I suspect that you ran code (a doc example?) that had something like "rand('state',0)" in it.  That will change the default stream so that your defaultStream will no longer be the thing that the rand function uses.

Yes, the help says that the seed must be an integer.  But over the years MATLAB users have grown accustomed to typing sum(100*clock), and there didn't seem to be any point in requiring anyone to now start typing round(sum(100*clock)).  If you do what the help says, you'll be fine.  But if you deviate slightly because of historical reasons, you'll get something sensible (and consistent with behavior in the older syntax).</description>
		<content:encoded><![CDATA[<p>Joe, when I run that code I get what I expect - the first W is the same as the second.  You haven&#8217;t said what you&#8217;re doing between these two code snippets, but if you&#8217;re not getting what I get, then presumably that&#8217;s where to look.  Here&#8217;s what I suspect happened:</p>
<p>You created defaultStream by calling getDefaultStream.  But even though you named it &#8220;defaultStream&#8221; doesn&#8217;t mean that someone can&#8217;t change the default stream out from under you.  Your defaultStream will remain a valid stream, but not necessarily the <b>default</b> stream.  I suspect that you ran code (a doc example?) that had something like &#8220;rand(&#8217;state&#8217;,0)&#8221; in it.  That will change the default stream so that your defaultStream will no longer be the thing that the rand function uses.</p>
<p>Yes, the help says that the seed must be an integer.  But over the years MATLAB users have grown accustomed to typing sum(100*clock), and there didn&#8217;t seem to be any point in requiring anyone to now start typing round(sum(100*clock)).  If you do what the help says, you&#8217;ll be fine.  But if you deviate slightly because of historical reasons, you&#8217;ll get something sensible (and consistent with behavior in the older syntax).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Pohedra</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30066</link>
		<dc:creator>Joe Pohedra</dc:creator>
		<pubDate>Tue, 03 Mar 2009 16:32:43 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30066</guid>
		<description>I want to get different normal distributions for several simulation runs. I'm using this code:
%initialize the random number generator
RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(100*clock)));
%save the state of the random number stream to allow repeating results later
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save('RandState', 'savedState');
%size distribution
Wmean = [2.65 2.75 2.85];		
Wstd  = [0.05 0.05 0.05];
W = repmat(Wmean,1000,1) +...
	repmat(Wstd,1000,1).*randn(1000,size(Wmean,2));

Later, I try to repeat one of the simulations using:
%commands to restore the random number stream state from the saved mat file
load RandState.mat
defaultStream.State = savedState;
%size distribution
Wmean = [2.65 2.75 2.85];		
Wstd  = [0.05 0.05 0.05];
W = repmat(Wmean,1000,1) +...
	repmat(Wstd,1000,1).*randn(1000,size(Wmean,2));

But I do not get the same W distributions. What am I doing wrong?

Also, note that the help for RandStream says that the Seed must be an integer. But sum(100*clock) is not an integer.
Thanks,
Joe</description>
		<content:encoded><![CDATA[<p>I want to get different normal distributions for several simulation runs. I&#8217;m using this code:<br />
%initialize the random number generator<br />
RandStream.setDefaultStream(RandStream(&#8217;mt19937ar&#8217;,&#8217;seed&#8217;,sum(100*clock)));<br />
%save the state of the random number stream to allow repeating results later<br />
defaultStream = RandStream.getDefaultStream;<br />
savedState = defaultStream.State;<br />
save(&#8217;RandState&#8217;, &#8217;savedState&#8217;);<br />
%size distribution<br />
Wmean = [2.65 2.75 2.85];<br />
Wstd  = [0.05 0.05 0.05];<br />
W = repmat(Wmean,1000,1) +&#8230;<br />
	repmat(Wstd,1000,1).*randn(1000,size(Wmean,2));</p>
<p>Later, I try to repeat one of the simulations using:<br />
%commands to restore the random number stream state from the saved mat file<br />
load RandState.mat<br />
defaultStream.State = savedState;<br />
%size distribution<br />
Wmean = [2.65 2.75 2.85];<br />
Wstd  = [0.05 0.05 0.05];<br />
W = repmat(Wmean,1000,1) +&#8230;<br />
	repmat(Wstd,1000,1).*randn(1000,size(Wmean,2));</p>
<p>But I do not get the same W distributions. What am I doing wrong?</p>
<p>Also, note that the help for RandStream says that the Seed must be an integer. But sum(100*clock) is not an integer.<br />
Thanks,<br />
Joe</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30031</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 10 Feb 2009 16:43:30 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30031</guid>
		<description>Amir-

You will get the same answers either way, assuming you start at the same point in the RNG.  No difference at all.

--loren</description>
		<content:encoded><![CDATA[<p>Amir-</p>
<p>You will get the same answers either way, assuming you start at the same point in the RNG.  No difference at all.</p>
<p>&#8211;loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amir</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30030</link>
		<dc:creator>Amir</dc:creator>
		<pubDate>Tue, 10 Feb 2009 16:23:30 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30030</guid>
		<description>oops! I am sorry there was a typo. The correct form of my question is 

A) x1 = [rand(1) ; rand(1) ; rand(1)];

B) x2 = rand(3,1);</description>
		<content:encoded><![CDATA[<p>oops! I am sorry there was a typo. The correct form of my question is </p>
<p>A) x1 = [rand(1) ; rand(1) ; rand(1)];</p>
<p>B) x2 = rand(3,1);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amir</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30029</link>
		<dc:creator>Amir</dc:creator>
		<pubDate>Tue, 10 Feb 2009 16:21:54 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-30029</guid>
		<description>Thanks for all the information. I have a question that I hope you can answer me:

Is there any difference between the following two cases?

A) x1 = [rand(1) ; rand(2) ; rand(3)];

B) x2 = rand(3,1);

In other words, is the statistical distributions of the random numbers is different when you generate them all at once using rand(m,n) or generate them one after another using rand(n) but for m*n times.</description>
		<content:encoded><![CDATA[<p>Thanks for all the information. I have a question that I hope you can answer me:</p>
<p>Is there any difference between the following two cases?</p>
<p>A) x1 = [rand(1) ; rand(2) ; rand(3)];</p>
<p>B) x2 = rand(3,1);</p>
<p>In other words, is the statistical distributions of the random numbers is different when you generate them all at once using rand(m,n) or generate them one after another using rand(n) but for m*n times.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-29869</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Mon, 17 Nov 2008 18:39:10 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-29869</guid>
		<description>Noam, I'm not exactly sure what you're describing.  I would think that to reproduce something that happened "late" within an iteration, you'd have to start at the beginning of that iteration.  If somehow that's not the case, it seems like you might be able to use several substreams as "checkpoints" _within_ an iteration, rather than just once at the beginning.</description>
		<content:encoded><![CDATA[<p>Noam, I&#8217;m not exactly sure what you&#8217;re describing.  I would think that to reproduce something that happened &#8220;late&#8221; within an iteration, you&#8217;d have to start at the beginning of that iteration.  If somehow that&#8217;s not the case, it seems like you might be able to use several substreams as &#8220;checkpoints&#8221; _within_ an iteration, rather than just once at the beginning.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noam</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-29867</link>
		<dc:creator>Noam</dc:creator>
		<pubDate>Sat, 15 Nov 2008 15:21:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-29867</guid>
		<description>Hi Peter,
Thank you for the clear explanations.
There are certain situations where each iteration in a simulation uses many random numbers. For the most part, I only need to reproduce the run from the beginning of an iteration, so saving the seed before each iteration is sufficient. However, sometimes I notice that something interesting happened late in the iteration (I only "look" at the state of the simulation after each iteration), and it would be useful for me to start with the last seed and go BACKWARD, ie. generating the LAST 1000 random numbers before the end of the iteration. Right now, to do this I rerun the last iteration in a more verbose mode, and check what happened towards the end. But since each iteration can be quite time consuming, it would be nice to be able to simply reverse the stream. Is that possible?
Thanks,
Noam</description>
		<content:encoded><![CDATA[<p>Hi Peter,<br />
Thank you for the clear explanations.<br />
There are certain situations where each iteration in a simulation uses many random numbers. For the most part, I only need to reproduce the run from the beginning of an iteration, so saving the seed before each iteration is sufficient. However, sometimes I notice that something interesting happened late in the iteration (I only &#8220;look&#8221; at the state of the simulation after each iteration), and it would be useful for me to start with the last seed and go BACKWARD, ie. generating the LAST 1000 random numbers before the end of the iteration. Right now, to do this I rerun the last iteration in a more verbose mode, and check what happened towards the end. But since each iteration can be quite time consuming, it would be nice to be able to simply reverse the stream. Is that possible?<br />
Thanks,<br />
Noam</p>
]]></content:encoded>
	</item>
</channel>
</rss>
