Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe25.iad.POSTED!7564ea0f!not-for-mail
From: "Nasser Abbasi" <nma@12000.org>
Newsgroups: comp.soft-sys.matlab
References: <hb5ujd$3i5$1@fred.mathworks.com>
Subject: Re: problem in generating random numbers after compiling the m file
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3598
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350
X-EsetId: 321EA926BF2033386258
X-EsetScannerBuild: 5823
Message-ID: <ixvBm.45343$lR3.44256@newsfe25.iad>
NNTP-Posting-Host: ncdeodfefpjopplmihjclpliaacepnnh
X-Complaints-To: abuse@charter.net
X-Trace: cineoljmlfmnfokdadefjppgkgeilljaadplmigcahhdpcgbncdeodfefpjopplmmgdmmdanmgdmojgmeldfjilndbmklmdhoeggmpchongebilplogkbhnbpnelbkhfcggpekigbfidlfdi
NNTP-Posting-Date: Thu, 15 Oct 2009 02:11:26 UTC
Date: Wed, 14 Oct 2009 21:11:16 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:577390



"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