Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Simpler Control of Random Number Generation in MATLAB

Once again we're going to hear from guest blogger Peter Perkins, who is a statistical software developer here at The MathWorks.

MATLAB has had random numbers since the beginning. But not surprisingly, as the state of that art advanced, the original
tools in MATLAB were not really suitable to incorporate new ideas like parallel random number generation. So R2008b saw the
addition of the RandStream class, which was designed to support those new ideas, including new generator algorithms, multiple random number streams,
substreams, and parallel generation.

RandStream also fixed an old problem that most people didn't even know existed, where MATLAB code that reseeded or read/wrote the state of MATLAB's random number
generator using the pre-R2008b "control" syntaxes, such as

rand('seed',0); % may not do what you think!

didn't always have the effect you might have expected.

So the introduction of RandStream was a good thing, providing powerful new ways of using random numbers. The problem was, with power came a price: using RandStream resulted in more verbose and harder-to-understand MATLAB code, required at least some knowledge of MATLAB objects, and just
plain felt different than the old ways of doing things.

stream = RandStream('mt19937ar','Seed',5489); % MATLAB's start-up settings
RandStream.setGlobalStream(stream);

Lots of people, justifiably, gave us feedback more or less saying, "Hey! I just want to generate random numbers! It shouldn't
be like learning brain surgery!" We listened, and in R2011a, added a new function, rng, that allows you to simply, quickly, and intuitively control how MATLAB generates random numbers. rng is actually built on top of RandStream, so in that sense it isn't anything new. But it provides a simpler interface for the most common operations.

For example, to reinitialize MATLAB's random number generator to its default settings, you use this command

rng default

and to reinitialize it with the seed 54321, you use this

rng(54321);

You can also reseed it "unpredictably" using

rng shuffle

Definitely a simpler syntax, and yet there's still some potentially tricky ideas going on there, ideas that can trip you up.
A couple of years ago, I wrote two posts describing the basic ideas behind random number generator seeds and states, and showed how to "control" the generator in
MATLAB, and more importantly, discussed when and when not to. All of that is still relevant and important to understand. But those posts explain it terms of RandStream.

This time around, I'll just point to the MATLAB User Guide section that covers much of the same ground, but this time in terms of the rng function. It is a quick read, and I really recommend it to anyone who wants to understand how to use random numbers in MATLAB.
The short summary is that quite often, you don't need to do anything special at all beyond just calling rand, randi, or randn to create random arrays. But if you need simple repeatability or independence, rng gets you that easily.

rng is also a more direct replacement than RandStream for the old, pre-R2008b ways of reseeding or reading/writing the generator state. If you have code that uses those old commands,
you've probably noticed that the Code Analyzer in the MATLAB editor flags them. In R2011a, the warning directs you to a section of the doc that shows how to replace the old with the new.

RandStream is still the tool to use for more complicated situations involving multiple parallel random streams, so rng doesn't entirely replace it. But for most people, most of the time, rng is a much simpler choice.

Published with MATLAB® 7.12


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.