MATLAB: How to generate same random number on Simulink and MATLAB

randrngsequencesimulink

I want to generate same sequence of random number on Simulink with 'Random Number' block and on MATLAB with 'randn'. How do I do that?

Best Answer

To generate random numbers that are repeatable, the two generators must use the same seed and generator algorithm. Currently 'Random Number' block uses Legacy MATLAB 4.0 generator to generate its sequence of numbers. For 'randn', you can choose among the various algorithms with 'rng'.
1) In Simulink, set the seed of the block to 0. Generate the number and save the variable in the workspace.
2) In MATLAB, set the seed and algorithm to 0 and 'v4', respectively.
>> rng(0,'v4')
>> matrandom = randn(101,1);
You can verify that the generated numbers are the same.