<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263160</link>
    <title>MATLAB Central Newsreader - problem in generating random numbers after compiling the m file</title>
    <description>Feed for thread: problem in generating random numbers after compiling the m file</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>Thu, 15 Oct 2009 01:41:01 -0400</pubDate>
      <title>problem in generating random numbers after compiling the m file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263160#687110</link>
      <author>Ci Griaal</author>
      <description>hi all &lt;br&gt;
I am sampling random numbers from a normal distribution.&lt;br&gt;
For example, if this is my simple code:&lt;br&gt;
&lt;br&gt;
clear all;clc;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;%x1=Normal distribution N(mean=100,sd=5)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;n=25;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;x1 = ( randn(n,1) * 0.2 ) + 1.2;&lt;br&gt;
%   &lt;br&gt;
%% write&lt;br&gt;
% Create a file&lt;br&gt;
fidfile='x1.csv';&lt;br&gt;
fid=fopen(fidfile,'w'); &lt;br&gt;
if (fid &amp;lt; 0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('could not open file &quot;x1.csv&quot;');&lt;br&gt;
end;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fprintf(fid, '%f\n', x1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fprintf(fid, '\n'); &lt;br&gt;
fclose(fid);&lt;br&gt;
&lt;br&gt;
This is running ok if i run it in Matlab environment (i.e. different set of values are generated every time I run the m file).&lt;br&gt;
However, if I compile it and run the executable file (.exe) it generates the same set of random numbers every time I run it. &lt;br&gt;
Could anyone help me with this bug please?</description>
    </item>
    <item>
      <pubDate>Thu, 15 Oct 2009 02:11:16 -0400</pubDate>
      <title>Re: problem in generating random numbers after compiling the m file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263160#687111</link>
      <author>Nasser Abbasi</author>
      <description>&lt;br&gt;
&quot;Ci Griaal&quot; &amp;lt;cigriaal@yahoo.com.au&amp;gt; wrote in message &lt;br&gt;
news:hb5ujd$3i5$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; hi all&lt;br&gt;
&amp;gt; I am sampling random numbers from a normal distribution.&lt;br&gt;
&amp;gt; For example, if this is my simple code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; clear all;clc;&lt;br&gt;
&amp;gt;   %x1=Normal distribution N(mean=100,sd=5)&lt;br&gt;
&amp;gt;   n=25;&lt;br&gt;
&amp;gt;   x1 = ( randn(n,1) * 0.2 ) + 1.2;&lt;br&gt;
&amp;gt; %&lt;br&gt;
&amp;gt; %% write&lt;br&gt;
&amp;gt; % Create a file&lt;br&gt;
&amp;gt; fidfile='x1.csv';&lt;br&gt;
&amp;gt; fid=fopen(fidfile,'w');&lt;br&gt;
&amp;gt; if (fid &amp;lt; 0)&lt;br&gt;
&amp;gt;    error('could not open file &quot;x1.csv&quot;');&lt;br&gt;
&amp;gt; end;&lt;br&gt;
&amp;gt;    fprintf(fid, '%f\n', x1);&lt;br&gt;
&amp;gt;    fprintf(fid, '\n');&lt;br&gt;
&amp;gt; fclose(fid);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; This is running ok if i run it in Matlab environment (i.e. different set &lt;br&gt;
&amp;gt; of values are generated every time I run the m file).&lt;br&gt;
&amp;gt; However, if I compile it and run the executable file (.exe) it generates &lt;br&gt;
&amp;gt; the same set of random numbers every time I run it.&lt;br&gt;
&amp;gt; Could anyone help me with this bug please?&lt;br&gt;
&lt;br&gt;
Not a bug I would think. When you run an .exe each time, the Matlab RTL &lt;br&gt;
(run-time library stuff) must initialize itself, and part of this is &lt;br&gt;
initializing the random number generator. May be something like this&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt;  reset(RandStream.getDefaultStream);&lt;br&gt;
K&amp;gt;&amp;gt; rand(5,1)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.8147&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9058&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1270&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9134&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.6324&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt; reset(RandStream.getDefaultStream);&lt;br&gt;
K&amp;gt;&amp;gt; rand(5,1)&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.8147&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9058&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.1270&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.9134&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.6324&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt;&lt;br&gt;
&lt;br&gt;
So each time you run your .exe, the RNG is reset to the same state and hence &lt;br&gt;
you'll obtain the same 'random' sequence again.&lt;br&gt;
&lt;br&gt;
It is useful sometime to be able to obtain the same random numbers each &lt;br&gt;
time, so you can compare results from different runs. But if that is not &lt;br&gt;
what you really want, then may be you should explicitly initialize RNG  or &lt;br&gt;
reset it using a seed which different than before (may be based on time or &lt;br&gt;
such), I am sure there is a way to do this if you read through help.&lt;br&gt;
&lt;br&gt;
--Nasser</description>
    </item>
    <item>
      <pubDate>Thu, 15 Oct 2009 02:31:02 -0400</pubDate>
      <title>Re: problem in generating random numbers after compiling the m file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/263160#687115</link>
      <author>Ci Griaal</author>
      <description>&quot;Nasser Abbasi&quot; &amp;lt;nma@12000.org&amp;gt; wrote in message &amp;lt;ixvBm.45343$lR3.44256@newsfe25.iad&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Ci Griaal&quot; &amp;lt;cigriaal@yahoo.com.au&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:hb5ujd$3i5$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &amp;gt; hi all&lt;br&gt;
&amp;gt; &amp;gt; I am sampling random numbers from a normal distribution.&lt;br&gt;
&amp;gt; &amp;gt; For example, if this is my simple code:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; clear all;clc;&lt;br&gt;
&amp;gt; &amp;gt;   %x1=Normal distribution N(mean=100,sd=5)&lt;br&gt;
&amp;gt; &amp;gt;   n=25;&lt;br&gt;
&amp;gt; &amp;gt;   x1 = ( randn(n,1) * 0.2 ) + 1.2;&lt;br&gt;
&amp;gt; &amp;gt; %&lt;br&gt;
&amp;gt; &amp;gt; %% write&lt;br&gt;
&amp;gt; &amp;gt; % Create a file&lt;br&gt;
&amp;gt; &amp;gt; fidfile='x1.csv';&lt;br&gt;
&amp;gt; &amp;gt; fid=fopen(fidfile,'w');&lt;br&gt;
&amp;gt; &amp;gt; if (fid &amp;lt; 0)&lt;br&gt;
&amp;gt; &amp;gt;    error('could not open file &quot;x1.csv&quot;');&lt;br&gt;
&amp;gt; &amp;gt; end;&lt;br&gt;
&amp;gt; &amp;gt;    fprintf(fid, '%f\n', x1);&lt;br&gt;
&amp;gt; &amp;gt;    fprintf(fid, '\n');&lt;br&gt;
&amp;gt; &amp;gt; fclose(fid);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; This is running ok if i run it in Matlab environment (i.e. different set &lt;br&gt;
&amp;gt; &amp;gt; of values are generated every time I run the m file).&lt;br&gt;
&amp;gt; &amp;gt; However, if I compile it and run the executable file (.exe) it generates &lt;br&gt;
&amp;gt; &amp;gt; the same set of random numbers every time I run it.&lt;br&gt;
&amp;gt; &amp;gt; Could anyone help me with this bug please?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Not a bug I would think. When you run an .exe each time, the Matlab RTL &lt;br&gt;
&amp;gt; (run-time library stuff) must initialize itself, and part of this is &lt;br&gt;
&amp;gt; initializing the random number generator. May be something like this&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt;  reset(RandStream.getDefaultStream);&lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt; rand(5,1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     0.8147&lt;br&gt;
&amp;gt;     0.9058&lt;br&gt;
&amp;gt;     0.1270&lt;br&gt;
&amp;gt;     0.9134&lt;br&gt;
&amp;gt;     0.6324&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt; reset(RandStream.getDefaultStream);&lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt; rand(5,1)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     0.8147&lt;br&gt;
&amp;gt;     0.9058&lt;br&gt;
&amp;gt;     0.1270&lt;br&gt;
&amp;gt;     0.9134&lt;br&gt;
&amp;gt;     0.6324&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So each time you run your .exe, the RNG is reset to the same state and hence &lt;br&gt;
&amp;gt; you'll obtain the same 'random' sequence again.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It is useful sometime to be able to obtain the same random numbers each &lt;br&gt;
&amp;gt; time, so you can compare results from different runs. But if that is not &lt;br&gt;
&amp;gt; what you really want, then may be you should explicitly initialize RNG  or &lt;br&gt;
&amp;gt; reset it using a seed which different than before (may be based on time or &lt;br&gt;
&amp;gt; such), I am sure there is a way to do this if you read through help.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; --Nasser&lt;br&gt;
&amp;gt; Thanks Nasser&lt;br&gt;
I found it on help. All solved</description>
    </item>
  </channel>
</rss>

