Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it necessary to call rng() twice in Matlab

Tags:

random

matlab

This seems like a bug to me. It seems like it is necessary to call rng() twice in Matlab to get the desired seeding. Consider the following experiments:

>> sd = rng(3) % THIS DOES NOT WORK

sd = 

     Type: 'twister'
     Seed: 0
    State: [625x1 uint32]

>> sd = rng(3) % BUT NOW IT DOES

sd = 

     Type: 'twister'
     Seed: 3
    State: [625x1 uint32]

>> sd = rng(3) % AND AGAIN, TO CONFIRM

sd = 

     Type: 'twister'
     Seed: 3
    State: [625x1 uint32]

>> sd = rng('shuffle') % BUT THIS FAILS

sd = 

     Type: 'twister'
     Seed: 3
    State: [625x1 uint32]

>> sd = rng('shuffle') % BUT ON THE SECOND GO IT WORKS

sd = 

     Type: 'twister'
     Seed: 87326715
    State: [625x1 uint32]

>> sd = rng('shuffle') % AND ON THE THIRD

sd = 

     Type: 'twister'
     Seed: 87326802
    State: [625x1 uint32]

>> sd = rng(4) % BUT AGAIN THIS FAILS

sd = 

     Type: 'twister'
     Seed: 87326987
    State: [625x1 uint32]

>> sd = rng(4) % BUT ON THE SECOND GO IT WORKS AGAIN

sd = 

     Type: 'twister'
     Seed: 4
    State: [625x1 uint32]

>> sd = rng(4) % AND SO ON

sd = 

     Type: 'twister'
     Seed: 4
    State: [625x1 uint32]
like image 445
Patrick Avatar asked Sep 19 '25 01:09

Patrick


1 Answers

Short answer: According to the documentation, the return value of rng is previous state:

sprev = rng(...) returns the previous settings of the random number generator used by rand, randi,and randn before changing the settings.

So, the answer is: no, it is not a bug. The random number generator was correctly initialized the first time you called it.

However, it is quite an unexpected behavior, in my opinion.


A bit longer answer: I suggest using RandStream objects instead, which are much more easy to comprehend for anyone with basic knowledge in object oriented programming. For instance:

s1 = RandStream.create('mrg32k3a');
r1 = rand(s1,100000,1);

I strongly suggest to avoid setting the global stream, as it has all of the disadvantages of global variables.

%Not recommended! (Due to global variable)
s = RandStream('mt19937ar','Seed',1);
RandStream.setGlobalStream(s);

Edit (1) I would like to explain my opinion about why setting the global random number generator is not a good practice. Basically, the purpose of any good software is to reduce the scope of any variable, in order to reduce coupling and increase cohesion. Global variable has the highest possible coupling (any routine may use it) and lowest possible cohesion. Global random number generator is even more risky than a normal variable, because it has much more chances to be used by someone else.

Re-seeding the global random number generator might lead to some bizarre errors. Consider the following example - you are writing a program that runs in for loop and generates a random number.

 for i=1:N
     k = randn(1,1); 
     %... Do something
 end

Everything seems to be perfect. Now you want to add a third party function Foo in the middle of your loop that does some stuff. The designer of the code decided to re-seed the global number generator to 1.

 for i=1:N
     k = randn(1,1); 
     %... Do something
     Foo();
 end

 function Foo()
     %Do some stuff
     rng(1);
 end

Surprise! Now your program generates a completely non-random sequence of numbers, namely, exactly the same number in each loop call.

Some more reading data, if you're still not convinced - Here, here and here

like image 112
Andrey Rubshtein Avatar answered Sep 22 '25 04:09

Andrey Rubshtein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!