Same sequence of random number generators than under Matlab?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Same sequence of random number generators than under Matlab?

Mathieu Dubois-2
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under
Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the
data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave
version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

martin_helm
Off top of my head I think you will not be able to get the same numbers
that way (the implementations are different).
I would create a large sequence of your random numbers in on of your
systems and save it to a file then read that as input into the other system.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Doug Stewart-4
In reply to this post by Mathieu Dubois-2


On Sun, Jun 3, 2012 at 10:03 AM, Mathieu Dubois <[hidden email]> wrote:
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave


I believe that the only way to do this is to generate a sequence of random numbers with one system (octave) and then store these to a file. Then use this file of numbers for both systems, this will give you exactly the same numbers for your test.

--
DAS

https://linuxcounter.net/user/206392.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

dersh
In reply to this post by Mathieu Dubois-2
Another option is to write some code to generate your own pseudo random sequence then use the same code on both.
There are plenty of algorithms out there if you look around.

--Adam

Doug Stewart <[hidden email]> wrote:



On Sun, Jun 3, 2012 at 10:03 AM, Mathieu Dubois <[hidden email]> wrote:
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave


I believe that the only way to do this is to generate a sequence of random numbers with one system (octave) and then store these to a file. Then use this file of numbers for both systems, this will give you exactly the same numbers for your test.

--
DAS

https://linuxcounter.net/user/206392.html

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

ahowe42
In reply to this post by Mathieu Dubois-2
I'm not sure if it will work, but your best best (if the randoms are reals) is to try setting the state to a certain value in both applications (rand('state',42) or randn('state',42)).  I'm not familiar with the seed algorithm the apps use, but even with the same starting state, and with both running on the same machine, the results could be different.  If, however, they are running on different machines, I'm sure they will not be identical.

If, however, the randoms are integers, perhaps you can generate a very large sequence in both and have the results be sufficiently similar.

Andrew
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Jordi Gutiérrez Hermoso-2
In reply to this post by Mathieu Dubois-2
On 3 June 2012 10:03, Mathieu Dubois <[hidden email]> wrote:
> In order to compare as precisely as possible the Matlab and Octave version
> it seems necessary to have the same sequence of random numbers.

This is impossible.

The exact algorithm for generating random numbers that Matlab uses is
hidden, as is their seed. We can't replicate their sequence.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Sergei Steshenko
In reply to this post by Mathieu Dubois-2




----- Original Message -----

> From: Mathieu Dubois <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Sunday, June 3, 2012 5:03 PM
> Subject: Same sequence of random number generators than under Matlab?
>
> Hello,
>
> I want to comapre the output of the Self-Organinzin Map algorithm under Matlab
> and Octave.
>
> The algorithm is stochastic because, as in most training algorithm, the data are
> presented inrandom order.
> In order to compare as precisely as possible the Matlab and Octave version it
> seems necessary to have the same sequence of random numbers.
>
> Is there a way to do that?
>
> I have tried the following:
> rand('twister', 10);
> s = rand(10, 1);
> disp(s);
> disp(mean(s));
> disp(var(s));
>
> The sequences are different (but the statistics are close).
>
> Mathieu
>
>

As others have already suggested, generate the sequence in one tool (either Matlab or Octave), store it in a file, and use it in the other tool.

Regards,
  Sergei.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Sergei Steshenko




----- Original Message -----

> From: Sergei Steshenko <[hidden email]>
> To: Mathieu Dubois <[hidden email]>; "[hidden email]" <[hidden email]>
> Cc:
> Sent: Sunday, June 3, 2012 8:45 PM
> Subject: Re: Same sequence of random number generators than under Matlab?
>
>
>
>
>
> ----- Original Message -----
>>  From: Mathieu Dubois <[hidden email]>
>>  To: [hidden email]
>>  Cc:
>>  Sent: Sunday, June 3, 2012 5:03 PM
>>  Subject: Same sequence of random number generators than under Matlab?
>>
>>  Hello,
>>
>>  I want to comapre the output of the Self-Organinzin Map algorithm under
> Matlab
>>  and Octave.
>>
>>  The algorithm is stochastic because, as in most training algorithm, the
> data are
>>  presented inrandom order.
>>  In order to compare as precisely as possible the Matlab and Octave version
> it
>>  seems necessary to have the same sequence of random numbers.
>>
>>  Is there a way to do that?
>>
>>  I have tried the following:
>>  rand('twister', 10);
>>  s = rand(10, 1);
>>  disp(s);
>>  disp(mean(s));
>>  disp(var(s));
>>
>>  The sequences are different (but the statistics are close).
>>
>>  Mathieu
>>
>>
>
> As others have already suggested, generate the sequence in one tool (either
> Matlab or Octave), store it in a file, and use it in the other tool.
>
> Regards,
>   Sergei.

In general, regardless of tools, using truly random numbers directly, without first storing them is a persistent storage, is methodologically wrong.

Suppose certain random numbers sequence exposes an anomaly.

If the sequence is not stored in some kind of persistent storage, it can't be replayed in order to investigate the anomaly.

Regards,
  Sergei.





>
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Mathieu Dubois-2
In reply to this post by martin_helm
Le 03/06/2012 16:19, Martin Helm a écrit :
> Off top of my head I think you will not be able to get the same numbers
> that way (the implementations are different).
> I would create a large sequence of your random numbers in on of your
> systems and save it to a file then read that as input into the other system.

Thanks to every one who replied so quickly. I will try this solution.

Mathieu

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Alasdair McAndrew
In reply to this post by Mathieu Dubois-2
Writing your own is a good idea.  Cleve Moler, here, speaks of the linear congruential generator 

x_{k+1} = ax_k (mod m)

with a,m = 16807, 2^31-1

which was in fact Matlab's internal random number generator until replaced by the Mersenne Twister.  You'll find some good links here; there are some PRNGs developed by the late George Marsaglia which are both far simpler, and have a far longer period, than the Mersenne Twister. 

For a Matlab implementation of the Wichmann-Hill PRNG (which works basically by adding up three floating point values and taking the fractional part as output), seehere.   This is a simple program which should work equally well in Matlab and Octave.

-Alasdair


On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois <[hidden email]> wrote:
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Alasdair McAndrew
Here's a simple function which generates n pseudo-random numbers according to the 2006 Wichmann-Hill method, which is much stronger than the one linked to above, and which passes the "Big Crush" test, as well as having a period of 2^121:

whrandom.m
-----------------

# Implements the 2006 Wichmann-Hill PRNG
# Computes n pseudo-random numbers starting with a seed of 4 user-chosen
# values.

function out = whrandom(ix,iy,iz,iw,n)
  p1 = 2147483579;
  p2 = 2147483543;
  p3 = 2147483423;
  p4 = 2147483123;
  out = zeros(n,1);
  for i=1:n
    ix = mod(11600*ix,p1);
    iy = mod(47003*iy,p2);
    iz = mod(23000*iz,p3);
    iw = mod(30000*iw,p4);
    out(i) = mod(ix/p1+iy/p2+iz/p3+iw/p4,1);
  end
-----------------

-Alasdair

On Mon, Jun 4, 2012 at 10:26 PM, Alasdair McAndrew <[hidden email]> wrote:
Writing your own is a good idea.  Cleve Moler, here, speaks of the linear congruential generator 

x_{k+1} = ax_k (mod m)

with a,m = 16807, 2^31-1

which was in fact Matlab's internal random number generator until replaced by the Mersenne Twister.  You'll find some good links here; there are some PRNGs developed by the late George Marsaglia which are both far simpler, and have a far longer period, than the Mersenne Twister. 

For a Matlab implementation of the Wichmann-Hill PRNG (which works basically by adding up three floating point values and taking the fractional part as output), seehere.   This is a simple program which should work equally well in Matlab and Octave.

-Alasdair


On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois <[hidden email]> wrote:
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave






--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Alasdair McAndrew
.. and here's a simple implementation of Marsaglia's "Complementary Multiply with Carry" generator.  It has a period of 2^131104 - far greater than that of the Mersenne Twister.  

--------------------
function out = CMWC32(s1,s2,n) # 32 bit CMWC
  a = 18782;
  c = s1;
  x = s2;
  b = 2^32-1;
  # initialize state vector
  s = zeros(4097,1);
  for i=1:4096
    x = b-bitand(uint64(a*x+c),b);
    c = bitshift(uint64(a*x+c),-32);
    s(i) = x;
  endfor
  # Now compute new values for use
  for i = 1:n
    x = s(1);
    s(4097) = b-bitand(uint64(a*x+c),b);
    c = bitshift(uint64(a*x+c),-32);
    disp(double(s(4097))/2^32)
    s = s(2:4097);
   endfor
--------------------
No doubt this could be streamlined and optimized considerably.  For the very simple C code which describes this PRNG, see here.

-Alasdair

On Tue, Jun 5, 2012 at 12:25 AM, Alasdair McAndrew <[hidden email]> wrote:
Here's a simple function which generates n pseudo-random numbers according to the 2006 Wichmann-Hill method, which is much stronger than the one linked to above, and which passes the "Big Crush" test, as well as having a period of 2^121:

whrandom.m
-----------------

# Implements the 2006 Wichmann-Hill PRNG
# Computes n pseudo-random numbers starting with a seed of 4 user-chosen
# values.

function out = whrandom(ix,iy,iz,iw,n)
  p1 = <a href="tel:2147483579" value="+12147483579" target="_blank">2147483579;
  p2 = <a href="tel:2147483543" value="+12147483543" target="_blank">2147483543;
  p3 = <a href="tel:2147483423" value="+12147483423" target="_blank">2147483423;
  p4 = <a href="tel:2147483123" value="+12147483123" target="_blank">2147483123;
  out = zeros(n,1);
  for i=1:n
    ix = mod(11600*ix,p1);
    iy = mod(47003*iy,p2);
    iz = mod(23000*iz,p3);
    iw = mod(30000*iw,p4);
    out(i) = mod(ix/p1+iy/p2+iz/p3+iw/p4,1);
  end
-----------------

-Alasdair

On Mon, Jun 4, 2012 at 10:26 PM, Alasdair McAndrew <[hidden email]> wrote:
Writing your own is a good idea.  Cleve Moler, here, speaks of the linear congruential generator 

x_{k+1} = ax_k (mod m)

with a,m = 16807, 2^31-1

which was in fact Matlab's internal random number generator until replaced by the Mersenne Twister.  You'll find some good links here; there are some PRNGs developed by the late George Marsaglia which are both far simpler, and have a far longer period, than the Mersenne Twister. 

For a Matlab implementation of the Wichmann-Hill PRNG (which works basically by adding up three floating point values and taking the fractional part as output), seehere.   This is a simple program which should work equally well in Matlab and Octave.

-Alasdair


On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois <[hidden email]> wrote:
Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave






--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Mathieu Dubois
In reply to this post by Alasdair McAndrew


----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?


> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

Following your advice, I was trying to implement the LCG but I can't find the document. Could you repost it?

Also thanks for the other algorithm but I want to use a simple one.

Mathieu

On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois < [hidden email] > wrote:


Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


______________________________ _________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/ listinfo/help-octave




--
Blog: http://amca01.wordpress.com 
Web: http://sites.google.com/site/amca01/ 
Facebook: http://www.facebook.com/alasdair.mcandrew 
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Francesco Potortì
In reply to this post by dersh
>Another option is to write some code to generate your own pseudo random sequence then use the same code on both.
>There are plenty of algorithms out there if you look around.

I think this is the best option.

--
Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Mathieu Dubois
In reply to this post by Alasdair McAndrew
----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?


> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

I tried to implement the algorithm (see attached file) but it doesn't seem to work (I get sequences containing 0). Can somebody help?

Just a note: I'm trying to mimck the behaviour of Matlab rand function. My plan is to put the file in the test directory (under the name 'rand.m') so that the rand function will be replaced by my implementation. This is because I don't want to touch the code of the toolbox I'm using. As this toolbox uses rand('state', seed) my code tries to mimick that (this is also why I don't want ti implement more complicated algorithm that use a vector of seeds). The code uses a global variable for the seed and for xn which is the next random number to return. I print the state of the genrator at the begining and end of the function.

Thanks in advance for any help,
Mathieu
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

my_rand.m (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Alasdair McAndrew
In reply to this post by Mathieu Dubois
The article you want is at http://www.mathworks.com.au/moler/random.pdf

and the LCG (very simple) is

x(n+1) = 16807*x(n) mod 2^31-1 with x(1)=1.

You can see Moler's m-file here:


-Alasdair

On Tue, Jun 5, 2012 at 7:21 PM, Mathieu Dubois <[hidden email]> wrote:


----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?


> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

Following your advice, I was trying to implement the LCG but I can't find the document. Could you repost it?

Also thanks for the other algorithm but I want to use a simple one.

Mathieu

On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois < [hidden email] > wrote:


Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


______________________________ _________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/ listinfo/help-octave




--
Blog: http://amca01.wordpress.com
Web: http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Alasdair McAndrew
You won't have much luck mimicking Matlab's rand function, as Matlab uses the Mersenne twister PRNG, which is very good, but quite complicated.  And because you don't have access to the source code, you can't be sure of how it's been set up to work.

Assuming you don't want too many random numbers, the simple LCG should suffice.

-Alasdair

On Tue, Jun 5, 2012 at 10:01 PM, Alasdair McAndrew <[hidden email]> wrote:
The article you want is at http://www.mathworks.com.au/moler/random.pdf

and the LCG (very simple) is

x(n+1) = 16807*x(n) mod 2^31-1 with x(1)=1.

You can see Moler's m-file here:


-Alasdair


On Tue, Jun 5, 2012 at 7:21 PM, Mathieu Dubois <[hidden email]> wrote:


----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?


> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

Following your advice, I was trying to implement the LCG but I can't find the document. Could you repost it?

Also thanks for the other algorithm but I want to use a simple one.

Mathieu

On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois < [hidden email] > wrote:


Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


______________________________ _________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/ listinfo/help-octave




--
Blog: http://amca01.wordpress.com
Web: http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Doug Stewart-4


On Tue, Jun 5, 2012 at 8:08 AM, Alasdair McAndrew <[hidden email]> wrote:
You won't have much luck mimicking Matlab's rand function, as Matlab uses the Mersenne twister PRNG, which is very good, but quite complicated.  And because you don't have access to the source code, you can't be sure of how it's been set up to work.

Assuming you don't want too many random numbers, the simple LCG should suffice.

-Alasdair

On Tue, Jun 5, 2012 at 10:01 PM, Alasdair McAndrew <[hidden email]> wrote:
The article you want is at http://www.mathworks.com.au/moler/random.pdf

and the LCG (very simple) is

x(n+1) = 16807*x(n) mod 2^31-1 with x(1)=1.

You can see Moler's m-file here:


-Alasdair


On Tue, Jun 5, 2012 at 7:21 PM, Mathieu Dubois <[hidden email]> wrote:


----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?


> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

Following your advice, I was trying to implement the LCG but I can't find the document. Could you repost it?

Also thanks for the other algorithm but I want to use a simple one.

Mathieu

On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois < [hidden email] > wrote:


Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu



Alasdair, you are correct in what you said, but my understanding of what he is going to do is this.

Make his own rand.m and use it in Matlab and Octave without changing any other code.
I think this is a good plan.


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

Mathieu Dubois
In reply to this post by Alasdair McAndrew


----- Mail original -----
De: "Alasdair McAndrew" <[hidden email]>
À: "Mathieu Dubois" <[hidden email]>, [hidden email]
Envoyé: Mardi 5 Juin 2012 14:08:20
Objet: Re: Same sequence of random number generators than under Matlab?

> You won't have much luck mimicking Matlab's rand function, as Matlab uses the Mersenne twister PRNG, which is very good, but quite complicated. And because you don't have access to the source code, you can't be sure of how it's been set up to work.

I mean to mimick the interface of the function (i.e. the response to calls such as rand('state', 10)), not the result.

> Assuming you don't want too many random numbers, the simple LCG should suffice.

Thanks for the code it really helps! I thinks it's working now.



-Alasdair


On Tue, Jun 5, 2012 at 10:01 PM, Alasdair McAndrew < [hidden email] > wrote:


The article you want is at http://www.mathworks.com.au/moler/random.pdf 


and the LCG (very simple) is


x(n+1) = 16807*x(n) mod 2^31-1 with x(1)=1.


You can see Moler's m-file here:


http://www.mathworks.com/matlabcentral/fileexchange/4822-using-numerical-computing-with-matlab-in-the-classroom/content/randmcg.m 


-Alasdair




On Tue, Jun 5, 2012 at 7:21 PM, Mathieu Dubois < [hidden email] > wrote:




----- Mail original -----
De: "Alasdair McAndrew" < [hidden email] >
À: "Mathieu Dubois" < [hidden email] >, [hidden email]
Envoyé: Lundi 4 Juin 2012 14:26:00
Objet: Re: Same sequence of random number generators than under Matlab?



> Writing your own is a good idea. Cleve Moler, here, speaks of the linear congruential generator


> x_{k+1} = ax_k (mod m)


> with a,m = 16807, 2^31-1

Following your advice, I was trying to implement the LCG but I can't find the document. Could you repost it?

Also thanks for the other algorithm but I want to use a simple one.

Mathieu



On Mon, Jun 4, 2012 at 12:03 AM, Mathieu Dubois < [hidden email] > wrote:


Hello,

I want to comapre the output of the Self-Organinzin Map algorithm under Matlab and Octave.

The algorithm is stochastic because, as in most training algorithm, the data are presented inrandom order.
In order to compare as precisely as possible the Matlab and Octave version it seems necessary to have the same sequence of random numbers.

Is there a way to do that?

I have tried the following:
rand('twister', 10);
s = rand(10, 1);
disp(s);
disp(mean(s));
disp(var(s));

The sequences are different (but the statistics are close).

Mathieu


______________________________ _________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/ listinfo/help-octave




--
Blog: http://amca01.wordpress.com 
Web: http://sites.google.com/site/amca01/ 
Facebook: http://www.facebook.com/alasdair.mcandrew 




--
Blog: http://amca01.wordpress.com 
Web: http://sites.google.com/site/amca01/ 
Facebook: http://www.facebook.com/alasdair.mcandrew 




--
Blog: http://amca01.wordpress.com 
Web: http://sites.google.com/site/amca01/ 
Facebook: http://www.facebook.com/alasdair.mcandrew 
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Same sequence of random number generators than under Matlab?

David Bateman-2
Why no just make Octave's rand/randn work in Matlab. This is the third
or fourth time I've suggested this over the year with code I posted on
the octave-forge list in 2005 as a mex-file.

http://sourceforge.net/mailarchive/forum.php?thread_name=425A617A.3010004%40motorola.com&forum_name=octave-dev

I've updated this for newer versions of Octave and attach it here.
Compile this file taking the randmtzig.{c,h} files from the Octave
sources. Copy the resulting file to rand.MEXEXT and randn.MEXEXT (where
MEXEXT is returned by the mexext function in matlab) and try something like

rand("state",1:3); rand(1,3)
randn("state",1:3);randn(1,3)

in Matlab and Octave and you should get the same results.

D.


--
David Bateman                                [hidden email]
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave

rand.c (3K) Download Attachment
12