How does wblrnd generate random numbers?

3 views (last 30 days)
Hello,
I was wondering what happens when I set rng(52) and then I call wblrnd four times to generate 4 different numbers.
rng(52); % Setting the seed for random number generation
a = zeros(1, 5);
b = zeros(1, 5);
for i = 1:5
a(i) = wblrnd(4, 3); % Generate random number for a
b(i) = wblrnd(4, 3); % Generate random number for b
end
Each time the code runs, a and b generate the same number. However, the values for a and b are different. In this case, is there a seperate seed for each time wblrand is called? How does the random number generation work behind the hood?

Accepted Answer

Walter Roberson
Walter Roberson on 8 Mar 2024
Each time a random number is generated, the current seed changes to another seed, in a complicated but deterministic manner.
rng(52); % Setting the seed for random number generation
You set the initial seed
for i = 1:5
a(i) = wblrnd(4, 3); % Generate random number for a
b(i) = wblrnd(4, 3); % Generate random number for b
end
when i is 1, the first random number is generated and stored in a(1) and the random state (deterministically) moves on to the second state. Then another random number is generated from the current state and is assigned to b(1) and the random state moves on to the third state. Then when i is 2, a random number is generated and assigned to a(2) and the random state moves on to the fourth state. Another random number is generated and assigned to b(2) and the random state moves on to the fifth state.
There is no reason why any of those random numbers should be equal to each other.
If you had done
rng(52); for i = 1 : 5; a(i) = wblrnd(4,3); end
rng(52); for i = 1 : 5; b(i) = wblrnd(4,3); end
then the random state would be initialized, a(1) would be generated, random state moves to second state, a(2) would be generated, random state moves on to third state, and so on. Then the random state would be reset, b(1) would be generated using the same conditions as applied for a(1) so b(1) would be the same, then the random state would move on to the second state, b(2) would be generated using the same random state as for a(2), and so on. In this situation, a and b come out the same.

More Answers (1)

Hassaan
Hassaan on 8 Mar 2024
Edited: Hassaan on 8 Mar 2024
rng(52); % Setting the seed for random number generation once for `a`
a = zeros(1, 5);
for i = 1:5
a(i) = wblrnd(4, 3); % Generate random number for a
end
rng(52); % Resetting the seed for random number generation for `b`
b = zeros(1, 5);
for i = 1:5
b(i) = wblrnd(4, 3); % Generate random number for b
end
disp([a])
2.3182 6.1560 4.6361 3.1332 5.2952
disp([b])
2.3182 6.1560 4.6361 3.1332 5.2952
This approach ensures that the random numbers in a and b are generated from the identical point in the random sequence, leading to identical values in a and b, provided that the parameters for wblrnd remain constant (in your case, both are 4, 3)
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!