Repetition of random numbers for variables with different length

5 views (last 30 days)
Dear all,
My code has different loops and different algorithms and I need to get always the same or part of the same sequence of random numbers. I have been used the command rng('default') or rng(0,'twister') in several parts of the code, but, unfortunatelly, it did not work.
Basically, in order to simply, I am doing a simple code below to illustrate what I am saying:
rng(0,'twister');
s=rng();
A=lhsdesign(2,2)
rng(s)
B=lhsdesign(1,2)
The results that I got are:
A =
0.021416525878527 0.099859765555600
0.757312175638579 0.929056830686392
B =
0.873013183706494 0.086624143860981
What I should expect is B equal to the first line of A. But, it is not. Why?
Could anyone help me to solve that?
Many thanks,
Odilon.

Answers (2)

Walter Roberson
Walter Roberson on 25 May 2020
random number generation is done down columns, so with (2,2) the order would be
1 3
2 4
When you then use (1,2) then the second random number generated would go into making the first value of the second variable.
But you do not see A(1,1) the same as B(1,1) because the algorithm uses several iterations and additional random numbers and does not treat the columns exactly the same as rows.
  2 Comments
Stephen23
Stephen23 on 26 May 2020
Odilon Rodrigues Filho's incorrectly posted "Answer" moved here:
Thank you very much, Walter.
Is there any way to make them (lhs) to be the same?
Thanks, Odilon
Stephen23
Stephen23 on 26 May 2020
Odilon Rodrigues Filho's incorrectly posted "Answer" moved here:
Thank you a lot, Walter, for your help. I will try to figure out how Can I solve that. Best, Odilon

Sign in to comment.


Odilon Rodrigues Filho
Odilon Rodrigues Filho on 25 May 2020
Thank you very much, Walter.
Is there any way to make them (lhs) to be the same?
Thanks, Odilon
  1 Comment
Walter Roberson
Walter Roberson on 25 May 2020
No. When you ask for more than one row, the rows are selected from different ranges each, and then the rows are shuffled. The range depends on the number of rows you asked for. The result for asking for two rows is not going to be the same as asking for one row.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!