Thread Subject: problem in generating random numbers after compiling the m file

Subject: problem in generating random numbers after compiling the m file

From: Ci Griaal

Date: 15 Oct, 2009 01:41:01

Message: 1 of 3

hi all
I am sampling random numbers from a normal distribution.
For example, if this is my simple code:

clear all;clc;
   %x1=Normal distribution N(mean=100,sd=5)
   n=25;
   x1 = ( randn(n,1) * 0.2 ) + 1.2;
%
%% write
% Create a file
fidfile='x1.csv';
fid=fopen(fidfile,'w');
if (fid < 0)
    error('could not open file "x1.csv"');
end;
    fprintf(fid, '%f\n', x1);
    fprintf(fid, '\n');
fclose(fid);

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).
However, if I compile it and run the executable file (.exe) it generates the same set of random numbers every time I run it.
Could anyone help me with this bug please?

Subject: problem in generating random numbers after compiling the m file

From: Nasser Abbasi

Date: 15 Oct, 2009 02:11:16

Message: 2 of 3


"Ci Griaal" <cigriaal@yahoo.com.au> wrote in message
news:hb5ujd$3i5$1@fred.mathworks.com...
> hi all
> I am sampling random numbers from a normal distribution.
> For example, if this is my simple code:
>
> clear all;clc;
> %x1=Normal distribution N(mean=100,sd=5)
> n=25;
> x1 = ( randn(n,1) * 0.2 ) + 1.2;
> %
> %% write
> % Create a file
> fidfile='x1.csv';
> fid=fopen(fidfile,'w');
> if (fid < 0)
> error('could not open file "x1.csv"');
> end;
> fprintf(fid, '%f\n', x1);
> fprintf(fid, '\n');
> fclose(fid);
>
> 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).
> However, if I compile it and run the executable file (.exe) it generates
> the same set of random numbers every time I run it.
> Could anyone help me with this bug please?

Not a bug I would think. When you run an .exe each time, the Matlab RTL
(run-time library stuff) must initialize itself, and part of this is
initializing the random number generator. May be something like this

K>> reset(RandStream.getDefaultStream);
K>> rand(5,1)

ans =

    0.8147
    0.9058
    0.1270
    0.9134
    0.6324

K>> reset(RandStream.getDefaultStream);
K>> rand(5,1)

ans =

    0.8147
    0.9058
    0.1270
    0.9134
    0.6324

K>>

So each time you run your .exe, the RNG is reset to the same state and hence
you'll obtain the same 'random' sequence again.

It is useful sometime to be able to obtain the same random numbers each
time, so you can compare results from different runs. But if that is not
what you really want, then may be you should explicitly initialize RNG or
reset it using a seed which different than before (may be based on time or
such), I am sure there is a way to do this if you read through help.

--Nasser

Subject: problem in generating random numbers after compiling the m file

From: Ci Griaal

Date: 15 Oct, 2009 02:31:02

Message: 3 of 3

"Nasser Abbasi" <nma@12000.org> wrote in message <ixvBm.45343$lR3.44256@newsfe25.iad>...
>
> "Ci Griaal" <cigriaal@yahoo.com.au> wrote in message
> news:hb5ujd$3i5$1@fred.mathworks.com...
> > hi all
> > I am sampling random numbers from a normal distribution.
> > For example, if this is my simple code:
> >
> > clear all;clc;
> > %x1=Normal distribution N(mean=100,sd=5)
> > n=25;
> > x1 = ( randn(n,1) * 0.2 ) + 1.2;
> > %
> > %% write
> > % Create a file
> > fidfile='x1.csv';
> > fid=fopen(fidfile,'w');
> > if (fid < 0)
> > error('could not open file "x1.csv"');
> > end;
> > fprintf(fid, '%f\n', x1);
> > fprintf(fid, '\n');
> > fclose(fid);
> >
> > 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).
> > However, if I compile it and run the executable file (.exe) it generates
> > the same set of random numbers every time I run it.
> > Could anyone help me with this bug please?
>
> Not a bug I would think. When you run an .exe each time, the Matlab RTL
> (run-time library stuff) must initialize itself, and part of this is
> initializing the random number generator. May be something like this
>
> K>> reset(RandStream.getDefaultStream);
> K>> rand(5,1)
>
> ans =
>
> 0.8147
> 0.9058
> 0.1270
> 0.9134
> 0.6324
>
> K>> reset(RandStream.getDefaultStream);
> K>> rand(5,1)
>
> ans =
>
> 0.8147
> 0.9058
> 0.1270
> 0.9134
> 0.6324
>
> K>>
>
> So each time you run your .exe, the RNG is reset to the same state and hence
> you'll obtain the same 'random' sequence again.
>
> It is useful sometime to be able to obtain the same random numbers each
> time, so you can compare results from different runs. But if that is not
> what you really want, then may be you should explicitly initialize RNG or
> reset it using a seed which different than before (may be based on time or
> such), I am sure there is a way to do this if you read through help.
>
> --Nasser
> Thanks Nasser
I found it on help. All solved

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com