Main Content

lognrnd

Lognormal random numbers

Description

example

r= lognrnd(mu,sigma)生成一个随机数的对数正态分配ibution with the distribution parametersmu(mean of logarithmic values) andsigma(standard deviation of logarithmic values).

r= lognrnd(mu,sigma,sz1,...,szN)generates an array of lognormal random numbers, wheresz1,...,szNindicates the size of each dimension.

example

r= lognrnd(mu,sigma,sz)generates an array of lognormal random numbers, where vectorszspecifiessize(r).

Examples

collapse all

Find the distribution parameters from the mean and variance of a lognormal distribution and generate a lognormal random value from the distribution.

Find the distribution parametersmuandsigmafrom the mean and variance.

m = 1;% meanv = 2;% variancemu = log((m^2)/sqrt(v+m^2))
mu = -0.5493
sigma = sqrt(log(v/(m^2)+1))
sigma = 1.0481

Generate a lognormal random value.

rng('default')% For reproducibilityr = lognrnd(mu,sigma)
r = 1.0144

Save the current state of the random number generator. Then create a 1-by-5 vector of lognormal random numbers from the lognormal distribution with the parameters 3 and 10.

s = rng; r = lognrnd(3,10,[1,5])
r =1×5109× 0.0000 1.8507 0.0000 0.0001 0.0000

Restore the state of the random number generator tos, and then create a new 1-by-5 vector of random numbers. The values are the same as before.

rng(s); r1 = lognrnd(3,10,[1,5])
r1 =1×5109× 0.0000 1.8507 0.0000 0.0001 0.0000

Create a matrix of lognormally distributed random numbers with the same size as an existing array.

A = [3 2; -2 1]; sz = size(A); R = lognrnd(0,1,sz)
R =2×21.7120 0.1045 6.2582 2.3683

You can combine the previous two lines of code into a single line.

R = lognrnd(1,0,size(A));

Input Arguments

collapse all

Mean of logarithmic values for the lognormal distribution, specified as a scalar value or an array of scalar values.

To generate random numbers from multiple distributions, specifymuandsigmausing arrays. If bothmuandsigmaare arrays, then the array sizes must be the same. If eithermuorsigmais a scalar, thenlognrndexpands the scalar argument into a constant array of the same size as the other argument.Each element inris the random number generated from the distribution specified by the corresponding elements inmuandsigma.

Example:[0 1 2; 0 1 2]

Data Types:single|double

Standard deviation of logarithmic values for the lognormal distribution, specified as a nonnegative scalar value or an array of nonnegative scalar values.

Ifsigmais zero, then the outputris always equal toexp(mu).

To generate random numbers from multiple distributions, specifymuandsigmausing arrays. If bothmuandsigmaare arrays, then the array sizes must be the same. If eithermuorsigmais a scalar, thenlognrndexpands the scalar argument into a constant array of the same size as the other argument.Each element inris the random number generated from the distribution specified by the corresponding elements inmuandsigma.

Example:[1 1 1; 2 2 2]

Data Types:single|double

Size of each dimension, specified as separate arguments of integers. For example, specifying5,3,2generates a 5-by-3-by-2 array of random numbers from the lognormal probability distribution.

If eithermuorsigmais an array, then the specified dimensionssz1,...,szNmust match the common dimensions ofmuandsigmaafter any necessary scalar expansion. The default values ofsz1,...,szNare the common dimensions.

  • If you specify a single valuesz1, thenris a square matrix of sizesz1-by-sz1.

  • If the size of any dimension is0or negative, thenris an empty array.

  • Beyond the second dimension,lognrndignores trailing dimensions with a size of 1. For example,lognrnd(mu,sigma,3,1,1,1)produces a 3-by-1 vector of random numbers.

Example:5,3,2

Data Types:single|double

Size of each dimension, specified as a row vector of integers. For example, specifying[5 3 2]generates a 5-by-3-by-2 array of random numbers from the lognormal probability distribution.

If eithermuorsigmais an array, then the specified dimensionsszmust match the common dimensions ofmuandsigmaafter any necessary scalar expansion. The default values ofszare the common dimensions.

  • If you specify a single value[sz1], thenris a square matrix of sizesz1-by-sz1.

  • If the size of any dimension is0or negative, thenris an empty array.

  • Beyond the second dimension,lognrndignores trailing dimensions with a size of 1. For example,lognrnd(mu,sigma,[3,1,1,1])produces a 3-by-1 vector of random numbers.

Example:[5 3 2]

Data Types:single|double

Output Arguments

collapse all

Lognormal random numbers, returned as a scalar value or an array of scalar values with the dimensions specified bysz1,...,szNorsz.Each element inris the random number generated from the distribution specified by the corresponding elements inmuandsigma.

More About

collapse all

Lognormal Distribution

The lognormal distribution is a probability distribution whose logarithm has a normal distribution.

The meanmand variancevof a lognormal random variable are functions of the lognormal distribution parametersµandσ:

m = exp ( μ + σ 2 / 2 ) v = exp ( 2 μ + σ 2 ) ( exp ( σ 2 ) 1 )

Also, you can compute the lognormal distribution parametersµandσfrom the meanmand variancev:

μ = log ( m 2 / v + m 2 ) σ = log ( v / m 2 + 1 )

Alternative Functionality

  • lognrndis a function specific to lognormal distribution. Statistics and Machine Learning Toolbox™ also offers the generic functionrandom支持各种概率金宝appability distributions. To userandom, create aLognormalDistributionprobability distribution object and pass the object as an input argument or specify the probability distribution name and its parameters. Note that the distribution-specific functionlognrndis faster than the generic functionrandom.

  • To generate random numbers interactively, userandtool, a user interface for random number generation.

References

[1] Marsaglia, G., and W. W. Tsang. “A Fast, Easily Implemented Method for Sampling from Decreasing or Symmetric Unimodal Density Functions.”SIAM Journal on Scientific and Statistical Computing. Vol. 5, Number 2, 1984, pp. 349–359.

[2] Evans, M., N. Hastings, and B. Peacock.Statistical Distributions. 2nd ed., Hoboken, NJ: John Wiley & Sons, Inc., 1993.

Extended Capabilities

Introduced before R2006a