| MATLAB Function Reference | ![]() |
Uniformly distributed random numbers and arrays
Syntax
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 s = rand('state')
Description
The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval (0,1).
Y = rand(n)
returns an n-by-n matrix of random entries. An error message appears if n is not a scalar.
Y = rand(m,n) or Y = rand([m n])
returns an m-by-n matrix of random entries.
Y = rand(m,n,p,...) or Y = rand([m n p...])
generates random arrays.
Y = rand(size(A))
returns an array of random entries that is the same size as A.
by itself, returns a scalar whose value changes each time it's referenced. rand,
s = rand('state')
returns a 35-element vector containing the current state of the uniform generator. To change the state of the generator:
Examples
Example 1. R = rand(3,4) may produce
This code makes a random choice between two equally probable alternatives.
Example 2. Generate 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 = 18.1106 10.6110 26.7460 43.5247 30.1125 17.9489 39.8714 43.8489 10.7856 38.3789 34.1517 27.8039 31.0061 37.2511 27.1557 20.8875 47.2726 18.1059 25.1792 22.1847 17.9526 28.6398 36.8855 43.2718 17.5861
See Also
randn, randperm, sprand, sprandn
| qz | randn | ![]() |
© 1994-2005 The MathWorks, Inc.