# All the random number functions of MATLAB

Source: Internet
Author: User
Tags rounds

All the random number functions of MATLAB

(i) MATLAB internal functions

A. basic random number

There are two basic functions for generating random numbers in MATLAB.

1 . rand ()

Generates a random variable that is evenly distributed over the (0,1) interval. Basic syntax:

RAND ([m,n,p ...])

Generate permutations into m*n*p ... A random number of multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Rand (5,1)% generates 5 random-number column vectors, typically in this format

RAND (5)% generates a random number matrix of 5 rows and 5 columns

RAND ([5,4])% generates a random number matrix of 5 rows and 4 columns

The approximate distribution of generated random numbers.

X=rand (100000,1);

hist (x,30);

From this you can see that the generated random number fits evenly. (The video tutorial will briefly mention the role of the Hist () function)

2 . randn ()

Generates random numbers that are subject to a standard normal distribution (mean 0, variance 1). The basic syntax is similar to RAND ().

RANDN ([m,n,p ...])

Generate permutations into m*n*p ... A random number of multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

RANDN (5,1)% generates 5 random number column vectors, typically in this format

RANDN (5)% generates a random number matrix of 5 rows and 5 columns

RANDN ([5,4])% generates a random number matrix of 5 rows and 4 columns

The approximate distribution of generated random numbers.

X=RANDN (100000,1);

hist (x,50);

It is possible to see that the generated random number conforms to the standard normal distribution.

B. continuous distribution random number

If you install the Statistics Toolbox (Statistic Toolbox), in addition to these two basic distributions, you can also use MATLAB intrinsics to generate random numbers that conform to these distributions.

3 . unifrnd ()

Similar to Rand (), this function generates a random number that is evenly distributed within an interval. Basic syntax

Unifrnd (a,b,[m,n,p,...])

The generated random number interval in (A, b), arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Unifrnd ( -2,3,5,1)% generates 5 random-number column vectors, typically in this format

Unifrnd ( -2,3,5)% generates a random number matrix of 5 rows and 5 columns

Unifrnd ( -2,3,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is within the ( -2,3) interval.

The approximate distribution of generated random numbers.

X=unifrnd ( -2,3,100000,1);

hist (x,50);

The graph can be seen to generate a random number that fits evenly across the interval ( -2,3).

4 . normrnd ()

Similar to RANDN (), this function generates a random number that specifies the normal distribution of the mean, standard deviation. Basic syntax

Normrnd (mu,sigma,[m,n,p,...])

The generated random number obeys the mean value mu, the standard deviation is sigma (note standard deviation is positive) normal distribution, these random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Normrnd (2,3,5,1)% generates 5 random number column vectors, typically in this format

Normrnd (2,3,5)% generates a random number matrix of 5 rows and 5 columns

Normrnd (2,3,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subject to a normal distribution of 2 and a standard deviation of 3.

The approximate distribution of generated random numbers.

X=normrnd (2,3,100000,1);

hist (x,50);

, the upper half is the approximate distribution of 100,000 random numbers generated by the previous line of statements with a mean value of 2, a standard deviation of 3, and the lower half is the approximate distribution of 100,000 standard normal distribution random numbers with the last statement in subsection "Randn ()".

Notice that the symmetry of the upper half of the image is shifted in a positive direction (exactly to x=2), which is the result of a mean value of 2.

Furthermore, since the standard deviation is 3, higher than the standard deviation (1) of the standard normal distribution, the upper part of the graph is fatter (note the difference in the x-axis scale).

5 . chi2rnd ()

This function generates random numbers that obey the chi-square (chi-square) distribution. The chi-square distribution has only one parameter: Freedom v. Basic syntax

Chi2rnd (v,[m,n,p,...])

The generated random number obeys the chi-square distribution of degrees of freedom, these random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Chi2rnd (5,5,1)% generates 5 random number column vectors, typically in this format

Chi2rnd (5,5)% generates a random number matrix of 5 rows and 5 columns

Chi2rnd (5,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subject to the Chi-square distribution of degrees of freedom is 5

The approximate distribution of generated random numbers.

X=chi2rnd (5,100000,1);

hist (x,50);

6 . frnd ()

This function generates random numbers that obey the F distribution. F Distribution has 2 parameters: V1, v2. Basic syntax

Frnd (v1,v2,[m,n,p,...])

The generated random numbers obey the chi-square distribution of the parameters (V1,V2), which are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Frnd (3,5,5,1)% generates 5 random number column vectors, typically in this format

Frnd (3,5,5)% generates a random number matrix of 5 rows and 5 columns

Frnd (3,5,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subject to the F Distribution of (v1=3,v2=5)

The approximate distribution of generated random numbers.

X=frnd (3,5,100000,1);

hist (x,50);

As can be seen from the results, the F distribution is centered on the left side of the x positive half axis, but it is also likely to have some values at extreme values.

7 . trnd ()

This function generates a random number that obeys T (Student's t distribution, where Student is not the meaning of the student, but rather the pseudonym of Cosset.w.s. The t distribution has 1 parameters: degrees of Freedom v. Basic syntax

Trnd (v,[m,n,p,...])

The generated random number obeys the T distribution with the parameter V, these random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Trnd (7,5,1)% generates 5 random number column vectors, typically in this format

Trnd (7,5)% generates a random number matrix of 5 rows and 5 columns

Trnd (7,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subject to the T distribution of (V=7)

The approximate distribution of generated random numbers.

X=trnd (7,100000,1);

hist (x,50);

It can be found that the T distribution is "thin" than the standard distribution, but as the degree of Freedom v increases, the t distribution becomes fatter, and when the degree of freedom is positive infinity, it turns into a standard normal distribution.

The following distributions are relatively less common, and the syntax of these functions is the same as the syntax of the preceding function, so it's a little bit easier to write--not in the video, you just have to follow the syntax of the previous distributions and there should be no difficulty--it's a good practice opportunity if you have enough time.

8 . betarnd ()

This function generates random numbers that follow the beta distribution. The beta distribution has two parameters, A and B, respectively. is a PDF graphic of A=2,b=5 's beta distribution.

The syntax for generating a beta distribution random number is:

Betarnd (a,b,[m,n,p,...])

9 . exprnd ()

This function generates random numbers that obey the exponential distribution. The exponential distribution has only one parameter: MU, which is a PDF graphic of the mu=3 exponential distribution

The syntax for generating exponential distribution random numbers is:

Betarnd (mu,[m,n,p,...])

Ten . gamrnd ()

Generates random numbers that obey the gamma distribution. Gamma distribution has two parameters: A and B. is a PDF graphic of the a=2,b=5 gamma distribution

The syntax for generating a gamma distribution random number is:

Gamrnd (a,b,[m,n,p,...])

One . lognrnd ()

Generates random numbers that obey a logarithmic normal distribution. It has two parameters: Mu and Sigma, followed by this random number to take the logarithm after the mean is MU, the standard deviation is the normal distribution of Sigma. Is the Mu=-1, sigma=1/1.2, logarithmic normal distribution of the PDF graphic.

The syntax for generating a random number of lognormal distributions is:

Lognrnd (mu,sigma,[m,n,p,...])

A . raylrnd ()

Generates random numbers that obey the Rayleigh (Rayleigh) distribution. Its distribution has 1 parameters: B. is a PDF graphic of B=2 's Rayleigh distribution.

The syntax for generating a Rayleigh distribution random number is:

Raylrnd (b,[m,n,p,...])

- . wblrnd ()

Generates random numbers that obey the Weibull (Weibull) distribution. Its distribution has 2 parameters: Scale parameter A and shape parameter B. is a PDF graphic of the Weibull distribution of a=3,b=2.

The syntax for generating weibull distributed random numbers is:

Wblrnd (a,b,[m,n,p,...])

There are also non-central Chi-square distributions (NCX2RND), non-central F-Distributions (NCFRND), non-center T Distributions (NCTRND), which are generated in parentheses that obey these distributions, using:

Help function number Name

Find.

c. discrete distributed random numbers

The random number of discrete distributions may be discrete and generally integer.

- . unidrnd ()

This function generates random numbers that obey discrete uniform distributions. Unifrnd is to select the real number evenly within a certain interval (either as a decimal or an integer), and the Unidrnd is to select the integer random numbers evenly. The discrete uniformly distributed random number has 1 parameters: N, which represents from {1, 2, 3, ... n} The n integers are sampled with the same probability. Basic syntax:

Unidrnd (n,[m,n,p,...])

These random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Unidrnd (5,5,1)% generates 5 random number column vectors, typically in this format

Unidrnd (5,5)% generates a random number matrix of 5 rows and 5 columns

Unidrnd (5,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subjected to a parameter of two distributions (10,0.3)

The approximate distribution of generated random numbers.

X=unidrnd (9,100000,1);

hist (x,9);

As can be seen, the probability of each integer is basically the same.

the . binornd ()

This function generates random numbers that obey the two-item distribution. Two items are distributed with 2 parameters: N,p. Consider a shooting example, each shot hit p, a total of shooting N gun, then the number of hits will be subject to the parameter (n,p) two distribution. Note that p is less than or equal to 1 and not negative, and N is an integer. Basic syntax:

Binornd (n,p,[m,n,p,...])

The generated random number obeys the two-item distribution with the parameter (n,p), and these random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Binornd (10,0.3,5,1)% generates 5 random number column vectors, typically in this format

Binornd (10,0.3,5)% generates a random number matrix of 5 rows and 5 columns

Binornd (10,0.3,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subjected to a parameter of two distributions (10,0.3)

The approximate distribution of generated random numbers.

X=binornd (10,0.45,100000,1);

hist (x,11);

We can explain this histogram, assuming that each shot hit rate of 0.45, each shot 10 times, a total of 100,000 rounds, this figure represents the 100,000 rounds of each round of hit results possible one situation.

- . geornd ()

This function generates random numbers that obey the geometric distribution. The geometric distribution has only one parameter: P. The practical significance of geometric distribution can be explained as the target hit rate is p, which is continuously targeted until the first hit goal is not hit the sum of times . Note that P is a probability, so it is less than or equal to 1 and not negative. Basic syntax:

Geornd (p,[m,n,p,...])

These random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Geornd (0.4,5,1)% generates 5 random number column vectors, typically in this format

Geornd (0.4,5)% generates a random number matrix of 5 rows and 5 columns

Geornd (0.4,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subjected to a two-item distribution with a parameter of (0.4)

The approximate distribution of generated random numbers.

X=geornd (0.4,100000,1);

hist (x,50);

- . poissrnd ()

This function generates random numbers that obey the Poisson (Poisson) distribution. The Poisson distribution has only one parameter: lambda. This parameter is greater than 0. Basic syntax:

Geornd (p,[m,n,p,...])

These random numbers are arranged into m*n*p ... Multidimensional vectors. If you write only M, the m*m matrix is generated, and if the argument is [M,n] you can omit the square brackets. Some examples:

Poissrnd (2,5,1)% generates 5 random number column vectors, typically in this format

Poissrnd (2,5)% generates a random number matrix of 5 rows and 5 columns

Poissrnd (2,[5,4])% generates a random number matrix of 5 rows and 4 columns

% Note: The random number generated by the above statement is subject to the Poisson distribution of (2)

The approximate distribution of generated random numbers.

X=poissrnd (2,100000,1);

hist (x,50);

Other discrete distributions also have hypergeometric distributions (hyper-geometric, functions are hygernd), and so on, see MATLAB help documentation in detail.

All the random number functions of MATLAB

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.