Random Number Generation in Matlab Programming
There are four basic fundamental random number functions available in MATLAB:
rand, randi, randn, and randperm.
The rand function returns real numbers between 0 and 1 that are drawn from a uniform
distribution in MATLAB. For example,
r1 = rand(1000,1);
r1 is a 1000-by-1 column vector containing real floating-point numbers drawn from a uniform
distribution. All the values in r1 are in the open interval (0, 1). Histogram of these values is
show roughly flat nature, which indicates a fairly uniform sampling of numbers.
The randi function gives back double integer values drawn from a discrete uniform
distribution.
r2 = randi(10,1000,1);
r2 is a 1000-by-1 column vector containing integer values drawn from a discrete uniform
distribution whose range is 1,2,...,10. A histogram of these values is also turnout to be
roughly flat in nature, which indicates a fairly uniform sampling of integers between 1 and 10.
The randn function gives arrays of real floating-point numbers that are made from a standard
normal distribution. For example,
r3 = randn(1000,1);
r3 written above is a 1000-by-1 column vector containing numbers drawn from a
standard normal distribution. Histogram of r3 comes like a roughly normal distribution whose
mean is 0 and standard deviation is 1.
The randperm function can be used to create arrays of random integer values that have no
repeated values. For example,
r4 = randperm(15,5);
r4 written above is a 1-by-5 array containing randomly selected integer values on the closed
interval, [1, 15]. Unlike randi, which can give an array containing repeated values, the array
returned by randperm has no repeated values.
Successive calls to any of above functions return different results. This behavior is helpful
for creating several different arrays of random values.