Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLABhas been retired 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 theRandStreamclass, which was designed to support those new ideas, including new generator algorithms, multiple random number streams,
substreams, and parallel generation.

RandStreamalso fixed an oldproblemthat 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 ofRandStreamwas a good thing, providing powerful new ways of using random numbers. The problem was, with power came a price: usingRandStreamresulted in more verbose and harder-to-understand MATLAB code, required at least some knowledge of MATLAB objects, and just
plainfelt differentthan the old ways of doing things.

stream = RandStream('mt19937ar','Seed',5489);% MATLAB's start-up settingsRandStream.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.rngis actually built on top ofRandStream, 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

rngdefault

and to reinitialize it with the seed54321, you use this

rng(54321);

You can also reseed it "unpredictably" using

rngshuffle

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 wrotetwo postsdescribing the basic ideas behind random number generator seeds and states, and showed how to "control" the generator in
MATLAB, and more importantly, discussed when andwhen notto. All of that is still relevant and important to understand. But those posts explain it terms ofRandStream.

This time around, I'll just point to theMATLAB User Guide sectionthat covers much of the same ground, but this time in terms of therngfunction. It is a quick read, and I really recommend it to anyone who wants to understand how to use random numbers in MATLAB.
短summary is that quite often,you don't need to do anything special at allbeyond just callingrand,randi, orrandnto create random arrays. But if you need simple repeatability or independence,rnggets you that easily.

rngis also a more direct replacement thanRandStreamfor 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 asection of the docthat shows how to replace the old with the new.

RandStreamis still the tool to use for more complicated situations involving multiple parallel random streams, sorngdoesn't entirely replace it. But for most people, most of the time,rngis a much simpler choice.

Published with MATLAB® 7.12

|