Matlab: Three forms of random number generated (Rand, Randi and Randn)

There are three kinds of library functions about random number generation in MATLAB. Let’s take a look at their forms:
1, Rand (…) 
it is to generate pseudo-random numbers with uniform distribution between 0 and 1 (open-loop, excluding 0 and 1), that is, infinite tests, in which the probability of each number is the same.
its function format is as follows:

 R = rand(N)              % Generate an N×N matrix of random numbers, where each element lies between 0 and 1
 R = rand([M,N,P,...])    % Generate M×N×P×... of matrix random numbers
 R = rand(M,N,P,...)      % As above, the brackets are not required
 R = rand(... , CLASSNAME) % Generate a random number of type CLASSNAME, e.g. 'double' or 'single' 

for example, generate a double type of 5 × 3 evenly distributed random number between 0 and 1:

R = rand(5,3,'double');

similarly, we want to generate 100 data between [a, b], which can be expressed as:

R = a + (b-a).*rand(100,1);

2.randi(…)
Randi (n) is a pseudo-random number evenly distributed among the generated (0, n), and the numbers are all integers, so each number is between 1 and n. It can be expressed in the following ways:

R = randi(iMax)            % Generate a uniformly distributed random number between 1:iMax
R = randi(iMax,m,n) % Generate a uniformly distributed random number between 1:iMax for m×n
R = randi([iMin,iMax],m,n) % Generate a uniformly distributed random number between iMin:iMax for m×n

for example:

R1 = randi(10,5,1); % Generate a 5×1 random number between 1:10
R2 = randi([10,20],2,3); % Generate a 2×3 random number between 10:20

3.randn(… )
sometimes we want to generate random numbers with normal distribution instead of random distribution, so we need to use randn function. The overall probability of the random number generated by it is normal distribution, the mean value is 0, and the variance is 1. That is to say, the probability of 0 in the generated number is the largest, and the more infinite or negative infinite the probability is smaller, but the random number may be all real numbers, but the probability of occurrence is different. Its format is as follows:

R = randn(N)   % Generate N x N normally distributed random numbers
R = randn(M,N) % Generate M×N normally distributed random numbers

for example:

R = randi(3); % Generate 3×3 normally distributed random numbers

4. Stable restart distribution RNG
here, let’s see how to make the random number generated by each program run the same, mainly with the help of the Lang function, and the format is as follows: 1

rng('default');
R = rand(1,5); % Generate a constant 1×5 random number per program run

in addition, there are expressions that generate the same distribution:

s = rng;
R1 = rand(1,5);
rng(s);
R2 = rand(1,5); % R1 and R2 random numbers are the same

Read More: