| MATLAB® | ![]() |
r = rand(n)
rand(m,n)
rand([m,n])
rand(m,n,p,...)
rand([m,n,p,...])
rand
rand(size(A))
r = rand(..., 'double')
r
= rand(..., 'single')
r = rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1). rand(m,n) or rand([m,n]) returns an m-by-n matrix. rand(m,n,p,...) or rand([m,n,p,...]) returns an m-by-n-by-p-by-... array. rand returns a scalar. rand(size(A)) returns an array the same size as A.
r = rand(..., 'double') or r = rand(..., 'single') returns an array of uniform values of the specified class.
Note Note: The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0. |
The sequence of numbers produced by rand is determined by the internal state of the uniform pseudorandom number generator that underlies rand, randi, and randn. The default random number stream properties can be set using @RandStream methods. See @RandStream for details about controlling the default stream.
Resetting the default stream to the same fixed state allows computations to be repeated. Setting the stream to different states leads to unique computations, however, it does not improve any statistical properties. Since the random number generator is initialized to the same state every time MATLAB software starts up, rand, randn, and randi will generate the same sequence of numbers in each session until the state is changed.
Note In versions of MATLAB prior to 7.7, you controlled the internal state of the random number stream used by rand by calling rand directly with the 'seed', 'state', or 'twister' keywords. That syntax is still supported for backwards compatibility, but is deprecated. For version 7.7, use the default stream as described in the @RandStream reference documentation. |
Generate values from the uniform distribution on the interval [a, b].
r = a + (b-a).*rand(100,1);
Replace the default stream at MATLAB startup, using a stream whose seed is based on clock, so that rand will return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session.
RandStream.setDefaultStream
(RandStream('mt19937ar','seed',sum(100*clock)));
rand(1,5)
Save the current state of the default stream, generate 5 values, restore the state, and repeat the sequence.
defaultStream = RandStream.getDefaultStream; savedState = defaultStream.State; u1 = rand(1,5) defaultStream.State = savedState; u2 = rand(1,5) % contains exactly the same values as u1
randi, randn, @RandStream, rand (RandStream), getDefaultStream (RandStream), sprand, sprandn, randperm
![]() | qz | rand (RandStream) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |