

Not only can you reseed the random number generator as shown above, you can also choose the type of random number generator that you want to use.
#Ark generator time calc code#
If you do need to reseed the generator, that is usually best done at the command line, or in a spot in your code that is not easily overlooked. Unless you need repeatability or uniqueness, it is usually advisable to simply generate random values without reseeding the generator. Īs with 'shuffle' there is a caveat when reseeding MATLAB's random number generator, because it affects all subsequent output from rand, randi, and randn. You can use the same seed several times, to repeat the same calculations. rng also provides a way to reseed it using a specific seed.
#Ark generator time calc how to#
So far, you've seen how to reset the random number generator to its default settings, and reseed it using a seed that is created using the current time. More Control over Repeatability and Non-Repeatability Bear in mind that if you use 'shuffle', you may want to save the seed that rng created so that you can repeat your calculations later on. Sometimes that is critical, sometimes it's just "nice", but often it is not important at all. What 'shuffle' does provide is a way to avoid repeating the same sequences of values. While it is perfectly fine to reseed the generator each time you start up MATLAB, or before you run some kind of large calculation involving random numbers, it is actually not a good idea to reseed the generator too frequently within a session, because this can affect the statistical properties of your random numbers. Choosing a seed based on the current time does not improve the statistical properties of the values you'll get from rand, randi, and randn, and does not make them "more random" in any real sense. For most purposes, though, it is not necessary to use 'shuffle' at all. You might think that it's a good idea, or even necessary, to use it to get "true" randomness in MATLAB. 'shuffle' is a very easy way to reseed the random number generator. rng gives you an easy way to do that, by creating a seed based on the current time. One simple way to avoid repeating the same random numbers in a new MATLAB session is to choose a different seed for the random number generator. So when it's necessary to combine calculations done in two or more MATLAB sessions as if they were statistically independent, you cannot use the default generator settings. Obviously, calculations that use the same "random" numbers cannot be thought of as statistically independent. But as mentioned above, each time you restart MATLAB those functions are reset and return the same sequences of numbers. Non-RepeatabilityĮach time you call rand, randi, or randn, they draw a new value from their shared random number generator, and successive values can be treated as statistically independent.

For now, it serves as a way to see what generator rand, randi, and randn are currently using. You'll see in more detail below how to use the above output, including the State field, to control and change how MATLAB generates random numbers.
