This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Why Do Random Numbers Repeat After Startup?

All the random number functions, rand, randn, randi, and randperm, draw values from a shared random number generator. Every time you start MATLAB®, the generator resets itself to the same state. Therefore, a command such as rand(2,2) returns the same result any time you execute it immediately following startup. Also, any script or function that calls the random number functions returns the same result whenever you restart.

If you want to avoid repeating the same random number arrays when MATLAB restarts, then execute the command,

before calling rand, randn, randi, or randperm. This command ensures that you do not repeat a result from a previous MATLAB session.

If you want to repeat a result that you got at the start of a MATLAB session without restarting, you can reset the generator to the startup state at any time using

When you execute rng('default'), the ensuing random number commands return results that match the output of a new MATLAB session. For example,
A = rand(2,2)
A =

    0.8147    0.1270
    0.9058    0.9134
The values in A match the output of rand(2,2) whenever you restart MATLAB.

See Also