Main Content

Controlling Random Number Generation

This example shows how to use therngfunction, which provides control over random number generation.

(Pseudo)Random numbers in MATLAB come from the兰德,兰德i, and兰德nfunctions. Many other functions call those three, but those are the fundamental building blocks. All three depend on a single shared random number generator that you can control usingrng.

这是important to realize that "random" numbers in MATLAB are not unpredictable at all, but are generated by a deterministic algorithm. The algorithm is designed to be sufficiently complicated so that its outputappearsto be an independent random sequence to someone who does not know the algorithm, and can pass various statistical tests of randomness. The function that is introduced here provides ways to take advantage of the determinism to

  • repeat calculations that involve random numbers, and get the same results, or

  • 保证不同兰德om numbers are used in repeated calculations

and to take advantage of the apparent randomness to justify combining results from separate calculations.

"Starting Over"

If you look at the output from兰德,兰德i, or兰德nin a new MATLAB session, you'll notice that they return the same sequences of numbers each time you restart MATLAB. It's often useful to be able to reset the random number generator to that startup state, without actually restarting MATLAB. For example, you might want to repeat a calculation that involves random numbers, and get the same result.

rngprovides a very simple way to put the random number generator back to its default settings.

rngdefault兰德% returns the same value as at startup
ans = 0.8147

What are the "default" random number settings that MATLAB starts up with, or thatrng defaultgives you? If you callrngwith no inputs, you can see that it is the Mersenne Twister generator algorithm, seeded with 0.

rng
ans =struct with fields:Type: 'twister' Seed: 0 State: [625x1 uint32]

You'll see in more detail below how to use the above output, including theStatefield, to control and change how MATLAB generates random numbers. For now, it serves as a way to see what generator兰德,兰德i, and兰德nare currently using.

Non-Repeatability

Each time you call兰德,兰德i, or兰德n, they draw a new value from their shared random number generator, and successive values can be treated as statistically independent. But as mentioned above, each time you restart MATLAB those functions are reset and return the same sequences of numbers. Obviously, calculations that use thesame"random" numbers cannot be thought of as statistically independent. So when it's necessary to combine calculations done in two or more MATLAB sessions as if theywerestatistically independent, you cannot use the default generator settings.

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.rnggives you an easy way to do that, by creating a seed based on the current time.

rngshuffle兰德
ans = 0.5971

Each time you use'shuffle', it reseeds the generator with a different seed. You can callrngwith no inputs to see what seed it actually used.

rng
ans =struct with fields:Type: 'twister' Seed: 1144302838 State: [625x1 uint32]
rngshuffle% creates a different seed each timerng
ans =struct with fields:Type: 'twister' Seed: 1144302848 State: [625x1 uint32]
兰德
ans = 0.8089

'shuffle'is a very easy way to reseed the random number generator. You might think that it's a good idea, or even necessary, to use it to get "true" randomness in MATLAB. For most purposes, though,it is not necessary to use'shuffle'at all. Choosing a seed based on the current time does not improve the statistical properties of the values you'll get from兰德,兰德i, and兰德n, and does not make them "more random" in any real sense. 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.

What'shuffle'都提供一种方法来避免重复一样的吗sequences of values. Sometimes that is critical, sometimes it's just "nice", but often it is not important at all. Bear in mind that if you use'shuffle', you may want to save the seed thatrngcreated so that you can repeat your calculations later on. You'll see how to do that below.

More Control over Repeatability and Non-Repeatability

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.rngalso provides a way to reseed it using a specific seed.

You can use the same seed several times, to repeat the same calculations. For example, if you run this code twice ...

rng(1)% the seed is any non-negative integer < 2^32x = randn(1,5)
x =1×5-0.6490 1.1812 -0.7585 -1.1096 -0.8456
rng(1) x = randn(1,5)
x =1×5-0.6490 1.1812 -0.7585 -1.1096 -0.8456

... you get exactly the same results. You might do this to recreatexafter having cleared it, so that you can repeat what happens in subsequent calculations that depend onx, using those specific values.

On the other hand, you might want to choosedifferentseeds to ensure that you don't repeat the same calculations. For example, if you run this code in one MATLAB session ...

rng(2) x2 = sum(randn(50,1000),1);% 1000 trials of a random walk

and this code in another ...

rng(3) x3 = sum(randn(50,1000),1);

... you could combine the two results and be confident that they are not simply the same results repeated twice.

x = [x2 x3];

As with'shuffle'there is a caveat when reseeding MATLAB's random number generator, because it affects all subsequent output from兰德,兰德i, and兰德n. Unless you need repeatability or uniqueness, it is usually advisable to simply generate random values without reseeding the generator. 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.

选择一个发电机类型

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. Different generator types produce different sequences of random numbers, and you might, for example, choose a specific type because of its statistical properties. Or you might need to recreate results from an older version of MATLAB that used a different default generator type.

One other common reason for choosing the generator type is that you are writing a validation test that generates "random" input data, and you need to guarantee that your test can always expect exactly the same predictable result. If you callrngwith a seed before creating the input data, it reseeds the random number generator. But if the generator type has been changed for some reason, then the output from兰德,兰德i, and兰德nwill not be what you expect from that seed. Therefore, to be 100% certain of repeatability, you can also specify a generator type.

For example,

rng(0,'twister')

causes兰德,兰德i, and兰德nto use the Mersenne Twister generator algorithm, after seeding it with 0.

Using'combRecursive'

rng(0,'combRecursive')

selects the Combined Multiple Recursive generator algorithm, which supports some parallel features that the Mersenne Twister does not.

This command

rng(0,'v4')

selects the generator algorithm that was the default in MATLAB 4.0.

And of course, this command returns the random number generator to its default settings.

rngdefault

However, because the default random number generator settings may change between MATLAB releases, using'default'does not guarantee predictable results over the long-term.'default'is a convenient way to reset the random number generator, but for even more predictability, specify a generator type and a seed.

On the other hand, when you are working interactively and need repeatability, it is simpler, and usually sufficient, to callrngwith just a seed.

Saving and Restoring Random Number Generator Settings

Callingrngwith no inputs returns a scalar structure with fields that contain two pieces of information described already: the generator type, and the integer with which the generator was last reseeded.

s = rng
s =struct with fields:Type: 'twister' Seed: 0 State: [625x1 uint32]

The third field,State, contains a copy of the generator's current state vector. This state vector is the information that the generator maintains internally in order to generate the next value in its sequence of random numbers. Each time you call兰德,兰德i, or兰德n, the generator that they share updates its internal state. Thus, the state vector in the settings structure returned byrngcontains the information necessary to repeat the sequence, beginning from the point at which the state was captured.

While just being able to see this output is informative,rngalso accepts a settings structure as aninput, so that you can save the settings, including the state vector, and restore them later to repeat calculations. Because the settings contain the generator type, you'll know exactly what you're getting, and so "later" might mean anything from moments later in the same MATLAB session, to years (and multiple MATLAB releases) later. You can repeat results from any point in the random number sequence at which you saved the generator settings. For example

x1 = randn(10,10);% move ahead in the random number sequences = rng;% save the settings at this pointx2 = randn(1,5)
x2 =1×50.8404 -0.8880 0.1001 -0.5445 0.3035
x3 = randn(5,5);% move ahead in the random number sequencerng(年代);% return the generator back to the saved statex2 = randn(1,5)% repeat the same numbers
x2 =1×50.8404 -0.8880 0.1001 -0.5445 0.3035

Notice that while reseeding provides only a coarse reinitialization, saving and restoring the generator state using the settings structure allows you to repeatanypart of the random number sequence.

The most common way to use a settings structure is to restore the generator state. However, because the structure contains not only the state, but also the generator type and seed, it's also a convenient way to temporarily switch generator types. For example, if you need to create values using one of the legacy generators from MATLAB 5.0, you can save the current settings at the same time that you switch to use the old generator ...

previousSettings = rng(0,'v5uniform')
previousSettings =struct with fields:Type: 'twister' Seed: 0 State: [625x1 uint32]

... and then restore the original settings later.

rng(previousSettings)

You should not modify the contents of any of the fields in a settings structure. In particular, you should not construct your own state vector, or even depend on the format of the generator state.

Writing Simpler, More Flexible, Code

rngallows you to either

  • reseed the random number generator, or

  • save and restore random number generator settings

without having to know what type it is. You can also return the random number generator to its default settings without having to know what those settings are. While there are situations when you mightwantto specify a generator type,rngaffords you the simplicity of nothavingto specify it.

If you are able to avoid specifying a generator type, your code will automatically adapt to cases where a different generator needs to be used, and will automatically benefit from improved properties in a new default random number generator type.

rngandRandStream

rngprovides a convenient way to control random number generation in MATLAB for the most common needs. However, more complicated situations involving multiple random number streams and parallel random number generation require a more complicated tool. TheRandStreamclass is that tool, and it provides the most powerful way to control random number generation. The two tools are complementary, withrngproviding a much simpler and concise syntax that is built on top of the flexibility ofRandStream.