<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: New Ways With Random Numbers, Part I</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/feed/" rel="self" type="application/rss+xml" />
	<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 MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:24:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Peter Perkins</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32912</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Tue, 17 Jan 2012 12:49:55 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32912</guid>
		<description>If all you want to do is _see_ it, just displaying the global stream will show you the seed:


&gt;&gt; RandStream.getGlobalStream
ans = 
mt19937ar random stream (current global stream)
             Seed: 0
  NormalTransform: Ziggurat


Note that getGlobalStream is a new name (in R2011a of the getDefaultStream method from earlier releases.  If you want to assign the value to a variable, it&#039;s the .Seed property of that stream.

In R2011a and later, there&#039;s a new simpler interface to controlling random numbers, for non-parallel cases:


&gt;&gt; rng
ans = 
     Type: &#039;twister&#039;
     Seed: 0
    State: [625x1 uint32]


It&#039;s described here : http://www.mathworks.com/help/techdoc/math/bs1qb_i.html</description>
		<content:encoded><![CDATA[<p>If all you want to do is _see_ it, just displaying the global stream will show you the seed:</p>
<p>&gt;&gt; RandStream.getGlobalStream<br />
ans =<br />
mt19937ar random stream (current global stream)<br />
             Seed: 0<br />
  NormalTransform: Ziggurat</p>
<p>Note that getGlobalStream is a new name (in R2011a of the getDefaultStream method from earlier releases.  If you want to assign the value to a variable, it&#8217;s the .Seed property of that stream.</p>
<p>In R2011a and later, there&#8217;s a new simpler interface to controlling random numbers, for non-parallel cases:</p>
<p>&gt;&gt; rng<br />
ans =<br />
     Type: &#8216;twister&#8217;<br />
     Seed: 0<br />
    State: [625x1 uint32]</p>
<p>It&#8217;s described here : <a href="http://www.mathworks.com/help/techdoc/math/bs1qb_i.html" rel="nofollow">http://www.mathworks.com/help/techdoc/math/bs1qb_i.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SAMITHA</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32902</link>
		<dc:creator>SAMITHA</dc:creator>
		<pubDate>Sat, 14 Jan 2012 01:34:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32902</guid>
		<description>How can I see the current seed in use?</description>
		<content:encoded><![CDATA[<p>How can I see the current seed in use?</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-32441</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Wed, 24 Aug 2011 16:18:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32441</guid>
		<description>Kamal, you are running into trouble because you are mixing two different syntaxes, things like

&lt;pre&gt;
stream = RandStream.getDefaultStream
&lt;/pre&gt;

and things like

&lt;pre&gt;
rand(’state’,11)
&lt;/pre&gt;

In this particular case, as I read your code, you&#039;re getting and using the default random number stream in your main code, but your function changes the default stream to something else, and that has global effects.  So when you get back to your main code, the default stream is no longer what it was when you saved its state, and while

&lt;pre&gt;
stream.State = savedState;
&lt;/pre&gt;

does indeed reset the stream&#039;s state, the subsequent call to rand DOESN&#039;T USE that stream because the calls to rand inside your function call have changed the default.

I don&#039;t know what you intend

&lt;pre&gt;
s= get(stream)
&lt;/pre&gt;

to do, but

&lt;pre&gt;
RandStream.setDefaultStream(stream)
&lt;/pre&gt;

is probably what you meant to do, and would solve the problem.

Some notes:

1) The calls like rand(’state’,11) use an old syntax, one that has not been recommended for several years, and one that you probably don&#039;t want to mix with calls to RandStream methods, not because it won&#039;t work, but because you will get confused.

2) The term &quot;default stream&quot; refers to &quot;the random number stream that rand/randi/randn use by default&quot;, but since RandStream was introduced in 2008, we found that the term is somewhat confusing.  So in R2011a, the doc describes this as &quot;the global random number stream&quot;, and the methods RandStream.get/setGlobalStream are now preferred.

3) I recommend that in your function, you create a local stream, and call rand using that to generate numbers.  This will not affect the global stream at all.  This strategy described in these blog posts.

4) If you are using R2011a, you may find that the new RNG function, described in another blog post here:

http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random-number-generation-in-matlab/

is simpler for your needs.  In fact, the second help example

http://www.mathworks.com/help/techdoc/ref/rng.html

does exactly what you want.</description>
		<content:encoded><![CDATA[<p>Kamal, you are running into trouble because you are mixing two different syntaxes, things like</p>
<pre>
stream = RandStream.getDefaultStream
</pre>
<p>and things like</p>
<pre>
rand(’state’,11)
</pre>
<p>In this particular case, as I read your code, you&#8217;re getting and using the default random number stream in your main code, but your function changes the default stream to something else, and that has global effects.  So when you get back to your main code, the default stream is no longer what it was when you saved its state, and while</p>
<pre>
stream.State = savedState;
</pre>
<p>does indeed reset the stream&#8217;s state, the subsequent call to rand DOESN&#8217;T USE that stream because the calls to rand inside your function call have changed the default.</p>
<p>I don&#8217;t know what you intend</p>
<pre>
s= get(stream)
</pre>
<p>to do, but</p>
<pre>
RandStream.setDefaultStream(stream)
</pre>
<p>is probably what you meant to do, and would solve the problem.</p>
<p>Some notes:</p>
<p>1) The calls like rand(’state’,11) use an old syntax, one that has not been recommended for several years, and one that you probably don&#8217;t want to mix with calls to RandStream methods, not because it won&#8217;t work, but because you will get confused.</p>
<p>2) The term &#8220;default stream&#8221; refers to &#8220;the random number stream that rand/randi/randn use by default&#8221;, but since RandStream was introduced in 2008, we found that the term is somewhat confusing.  So in R2011a, the doc describes this as &#8220;the global random number stream&#8221;, and the methods RandStream.get/setGlobalStream are now preferred.</p>
<p>3) I recommend that in your function, you create a local stream, and call rand using that to generate numbers.  This will not affect the global stream at all.  This strategy described in these blog posts.</p>
<p>4) If you are using R2011a, you may find that the new RNG function, described in another blog post here:</p>
<p><a href="http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random-number-generation-in-matlab/" rel="nofollow">http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random-number-generation-in-matlab/</a></p>
<p>is simpler for your needs.  In fact, the second help example</p>
<p><a href="http://www.mathworks.com/help/techdoc/ref/rng.html" rel="nofollow">http://www.mathworks.com/help/techdoc/ref/rng.html</a></p>
<p>does exactly what you want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamal Hossain</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32440</link>
		<dc:creator>Kamal Hossain</dc:creator>
		<pubDate>Wed, 24 Aug 2011 09:21:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-32440</guid>
		<description>I was wondering, I was using random number(rand command) to generate random values. At a point I have to call a function where I am using a fixed &#039;random state&#039; and after the function operation when I am back to the main program, I want to start from the random state where left from the main program. So how can I do that?
 I was trying to do this:

clc; clear; close all;
stream = RandStream.getDefaultStream
savedState = stream.State;
for 1=1:100
u1 = rand(1,5)
end

   %function call(using fixed random state)
rand(&#039;state&#039;,11)
x = rand(1,5)
rand(&#039;state&#039;,125)
y=rand(1,5)
 
  %back to the main program
s= get(stream)
stream.State = savedState;
for j=1:100
u2 = rand(1,5)
end

but the problem is when I come back to the main program I am getting the same random numbers as I get previously.

I would be glad to get your respond.</description>
		<content:encoded><![CDATA[<p>I was wondering, I was using random number(rand command) to generate random values. At a point I have to call a function where I am using a fixed &#8216;random state&#8217; and after the function operation when I am back to the main program, I want to start from the random state where left from the main program. So how can I do that?<br />
 I was trying to do this:</p>
<p>clc; clear; close all;<br />
stream = RandStream.getDefaultStream<br />
savedState = stream.State;<br />
for 1=1:100<br />
u1 = rand(1,5)<br />
end</p>
<p>   %function call(using fixed random state)<br />
rand(&#8216;state&#8217;,11)<br />
x = rand(1,5)<br />
rand(&#8216;state&#8217;,125)<br />
y=rand(1,5)</p>
<p>  %back to the main program<br />
s= get(stream)<br />
stream.State = savedState;<br />
for j=1:100<br />
u2 = rand(1,5)<br />
end</p>
<p>but the problem is when I come back to the main program I am getting the same random numbers as I get previously.</p>
<p>I would be glad to get your respond.</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-31682</link>
		<dc:creator>Peter Perkins</dc:creator>
		<pubDate>Mon, 20 Sep 2010 01:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-31682</guid>
		<description>Amin, I&#039;m afraid that you have not described what you want to do clearly enough.  If you are doing some kind of Monte-Carlo simulation, and you run 50,000 trials, and then run 10,000 trials, then of course the two answers will differ.</description>
		<content:encoded><![CDATA[<p>Amin, I&#8217;m afraid that you have not described what you want to do clearly enough.  If you are doing some kind of Monte-Carlo simulation, and you run 50,000 trials, and then run 10,000 trials, then of course the two answers will differ.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amin</title>
		<link>http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-31679</link>
		<dc:creator>Amin</dc:creator>
		<pubDate>Sun, 19 Sep 2010 13:26:38 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/#comment-31679</guid>
		<description>hi,many thanks for comments
i have a question:
i want to simulate a formula containing random numbers from gamma distribution and normal distribution
i want to simulate this formula 50000 times
if i use 10000 instead of 50000,are the results different?
if yes,is there any way to have same result?</description>
		<content:encoded><![CDATA[<p>hi,many thanks for comments<br />
i have a question:<br />
i want to simulate a formula containing random numbers from gamma distribution and normal distribution<br />
i want to simulate this formula 50000 times<br />
if i use 10000 instead of 50000,are the results different?<br />
if yes,is there any way to have same result?</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-30073</link>
		<dc:creator>Joe Pohedra</dc:creator>
		<pubDate>Tue, 03 Mar 2009 21:32:06 +0000</pubDate>
		<guid isPermaLink="false">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&#039;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 isPermaLink="false">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&#039;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 isPermaLink="false">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(&#039;mt19937ar&#039;,&#039;seed&#039;,savedSeed));
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
save(&#039;RandInit&#039;, &#039;savedSeed&#039;, &#039;savedState&#039;);

To re-generate the same distributions again later:
load RandInit.mat
RandStream.setDefaultStream(RandStream(&#039;mt19937ar&#039;,&#039;seed&#039;,savedSeed));
defaultStream = RandStream.getDefaultStream;
defaultStream.State = savedState;

I don&#039;t think I could have worked this out without the benefit of the descriptions and examples shown in the two parts of &quot;New Ways With Random Numbers&quot;.
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(&#8216;mt19937ar&#8217;,'seed&#8217;,savedSeed));<br />
defaultStream = RandStream.getDefaultStream;<br />
savedState = defaultStream.State;<br />
save(&#8216;RandInit&#8217;, &#8216;savedSeed&#8217;, &#8216;savedState&#8217;);</p>
<p>To re-generate the same distributions again later:<br />
load RandInit.mat<br />
RandStream.setDefaultStream(RandStream(&#8216;mt19937ar&#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 isPermaLink="false">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&#039;t said what you&#039;re doing between these two code snippets, but if you&#039;re not getting what I get, then presumably that&#039;s where to look.  Here&#039;s what I suspect happened:

You created defaultStream by calling getDefaultStream.  But even though you named it &quot;defaultStream&quot; doesn&#039;t mean that someone can&#039;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 &quot;rand(&#039;state&#039;,0)&quot; 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&#039;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&#039;ll be fine.  But if you deviate slightly because of historical reasons, you&#039;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 &#8211; 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(&#8216;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>
</channel>
</rss>

