| MATLAB Function Reference | ![]() |
Y = rand
Y = rand(n)
Y = rand(m,n)
Y = rand([m n])
Y = rand(m,n,p,...)
Y = rand([m n p...])
Y = rand(size(A))
rand(method,s)
s = rand(method)
Y = rand returns a pseudorandom, scalar value drawn from a uniform distribution on the unit interval.
Y = rand(n) returns an n-by-n matrix of values derived as described above.
Y = rand(m,n) or Y = rand([m n]) returns an m-by-n matrix of the same.
Y = rand(m,n,p,...) or Y = rand([m n p...]) generates an m-by-n-by-p-by-... array of the same.
Note The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0. |
Y = rand(size(A)) returns an array that is the same size as A.
rand(method,s) causes rand to use the generator determined by method, and initializes the state of that generator using the value of s.
The value of s is dependent upon which method is selected. If method is set to 'state' or 'twister', then s must be either a scalar integer value from 0 to 2^32-1 or the output of rand(method). If method is set to 'seed', then s must be either a scalar integer value from 0 to 2^31-2 or the output of rand(method).
The rand and randn generators each maintain their own internal state information. Initializing the state of one has no effect on the other.
Input argument method can be any of the strings shown in the table below:
method | Description |
|---|---|
'twister' | Use the Mersenne Twister algorithm by Nishimura and Matsumoto (the default in MATLAB® Versions 7.4 and later). This method generates double-precision values in the closed interval [2^(-53), 1-2^(-53)], with a period of (2^19937-1)/2. |
'state' | Use a modified version of Marsaglia's subtract with borrow algorithm (the default in MATLAB versions 5 through 7.3). This method can generate all the double-precision values in the closed interval [2^(-53), 1-2^(-53)]. It theoretically can generate over 2^1492 values before repeating itself. |
'seed' | Use a multiplicative congruential algorithm (the default in MATLAB version 4). This method generates double-precision values in the closed interval [1/(2^31-1), 1-1/(2^31-1)], with a period of 2^31-2. |
For a full description of the Mersenne twister algorithm, see
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
s = rand(method) returns in s the current internal state of the generator selected by method. It does not change the generator being used.
The sequence of numbers produced by rand is determined by the internal state of the generator. Setting the generator to the same fixed state enables you to repeat computations. Setting the generator to different states leads to unique computations. It does not, however, improve statistical properties.
Because MATLAB software resets the rand state at startup, rand generates the same sequence of numbers in each session unless you change the value of the state input.
Make a random choice between two equally probable alternatives:
if rand < .5
'heads'
else
'tails'
end Generate a 3-by-4 pseudorandom matrix:
R = rand(3,4)
R =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706Set rand to its default initial state:
rand('twister', 5489);Initialize rand to a different state each time:
rand('twister', sum(100*clock));Save the current state, generate 10000 values, reset the state, and repeat the sequence:
s = rand('twister');
u1 = rand(100);
rand('twister',s);
u2 = rand(100); % contains exactly the same values as u1Generate uniform integers on the set 1:n:
n = 75;
f = ceil(n.*rand(100,1));
f(1:10)
ans =
72
37
61
11
32
69
60
72
50
3Generate a uniform distribution of random numbers on a specified interval [a,b]. To do this, multiply the output of rand by (b-a), then add a. For example, to generate a 5-by-5 array of uniformly distributed random numbers on the interval [10,50],
a = 10; b = 50; x = a + (b-a) * rand(5) x = 19.1591 49.8454 10.1854 25.9913 17.2739 46.5335 13.1270 40.9964 20.3948 20.5521 16.0951 27.7071 42.6921 42.0027 15.8216 43.0327 14.2661 44.7478 27.2566 15.4427 31.5337 48.4759 13.3774 46.4259 44.7717
[1] Moler, C.B., "Numerical Computing with MATLAB," SIAM, (2004), 336 pp. Available online at http://www.mathworks.com/moler.
[2] G. Marsaglia and A. Zaman "A New Class of Random Number Generators," Annals of Applied Probability, (1991), 3:462-480.
[3] Matsumoto, M. and Nishimura, T. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudorandom Number Generator," ACM Transactions on Modeling and Computer Simulation, (1998), 8(1):3-30.
[4] Park, S.K. and Miller, K.W. "Random Number Generators: Good Ones Are Hard to Find," Communications of the ACM, (1988), 31(10):1192-1201
randn, randperm, sprand, sprandn
![]() | qz | randn | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |