Creates an array of pseudo-random numbers of the specified size.
The numbers are uniformly distributed on [0,1)
.
Two seperate syntaxes are possible. The first syntax specifies the array
dimensions as a sequence of scalar dimensions:
y = rand(d1,d2,...,dn).
The resulting array has the given dimensions, and is filled with
random numbers. The type of y
is double
, a 64-bit floating
point array. To get arrays of other types, use the typecast
functions.
The second syntax specifies the array dimensions as a vector, where each element in the vector specifies a dimension length:
y = rand([d1,d2,...,dn]).
This syntax is more convenient for calling rand
using a
variable for the argument.
The following example demonstrates an example of using the first form of the rand
function.
--> rand(2,2,2) ans = <double> - size: [2 2 2] (:,:,1) = Columns 1 to 2 0.814723686393179 0.126986816293506 0.905791937075619 0.913375856139019 (:,:,2) = Columns 1 to 2 0.632359246225410 0.278498218867048 0.0975404049994095 0.546881519204984
The second example demonstrates the second form of the rand
function.
--> rand([2,2,2]) ans = <double> - size: [2 2 2] (:,:,1) = Columns 1 to 2 0.814723686393179 0.126986816293506 0.905791937075619 0.913375856139019 (:,:,2) = Columns 1 to 2 0.632359246225410 0.278498218867048 0.0975404049994095 0.546881519204984
The third example computes the mean and variance of a large number of uniform random numbers. Recall that the mean should be 1/2
, and the variance should be 1/12 ~ 0.083
.
--> x = rand(1,10000); --> mean(x) ans = <double> - size: [1 1] 0.499560579911729 --> var(x) ans = <double> - size: [1 1] 0.0829099953434714