rng function in matlab
2 views (last 30 days)
Show older comments
Hi, I have 3 question about the rng function in matlab:
- what is porpuse of the state field?
- what does legacy type mean?
- what does 'shuffle' mean?
thanks/
2 Comments
Scott MacKenzie
on 22 Apr 2021
Have you tried
>> doc rng
The answers to your questions are in the documentation.
Answers (1)
Steven Lord
on 22 Apr 2021
what is porpuse of the state field?
It is the state of the random number generator. For most users of rng you can ignore what the elements of the state represents and just know what if you call rng with the same inputs (except for 'shuffle') it will reset the random number generator to the same condition, allowing it to generate the same numbers.
rng(1, 'twister')
x1 = rand(1, 10);
rng(1, 'twister')
x2 = rand(1, 10);
isequal(x1, x2) % true
If the output of rng was a machine it likely would be labeled "No user-serviceable parts inside".
what does legacy type mean?
The legacy random number generators are generators that were the defaults in older releases. Unless you're trying to reproduce the same results as were generated in those older releases you should probably use the newer generators. From the table in the "Choosing a Random Number Generator" section on this documentation page you can see that the legacy generators tend to have a much smaller period before they repeat and/or they have other characteristics that make them not the best choices.
what does 'shuffle' mean?
Use 'shuffle' when you want the random number generator to be in some arbitrary state but you don't care what state. The name is meant to evoke a deck of cards: if you're playing a game you don't really care what order the deck is in, but you don't want it to be stacked Ace through King of one suit, then A through K of the next suit, etc. So you give it a couple quick shuffles and call it good.
Calling rng(fixedSeed, generator) is like a magician shuffling the deck in a specific way to put it in a fixed order that they know ahead of time. This allows them to perform their card trick and get the same result each performance.
0 Comments
See Also
Categories
Find more on Random Number Generation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!