Documentation

random

Random numbers

Syntax

R = random('name',A)
R = random('name',A,B)
R = random('name',A,B,C)
R = random('name',A,B,C,D)
R = random(pd)
R = random(___,sz1,...,szN)
R = random(___,[sz1,...,szN])

Description

example

R= random('name',A)returns a random number from the one-parameter distribution family specified by'name'and the distribution parameterA.

example

R= random('name',A,B)returns a random number from the two-parameter distribution family specified by'name'and the distribution parametersAandB.

R= random('name',A,B,C)returns a random number from the three-parameter distribution family specified by'name'and the distribution parametersA,B, andC.

R= random('name',A,B,C,D)returns a random number from the four-parameter distribution family specified by'name'and the distribution parametersA,B,C, andD.

example

R= random(pd)returns a random number from the probability distribution objectpd.

example

R= random(___,sz1,...,szN)orR= random(___,[sz1,...,szN])generates asz1-by-⋯-by-szNarray of random numbers from the specified probability distribution using input arguments from any of the previous syntaxes.

Examples

collapse all

Create a standard normal probability distribution object.

pd = makedist('Normal')
pd = NormalDistribution Normal distribution mu = 0 sigma = 1

Generate one random number from the distribution.

rng('default')% For reproducibilityr1 = random(pd)
r1 = 0.5377

Alternatively, you can generate a standard normal random number by specifying its name and parameters.

r2 = random('Normal',0,1)
r2 = 1.8339

Save the current state of the random number generator. Then generate a random number from the Poisson distribution with rate parameter 5.

s = rng; r = random('Poisson',5)
r = 5

Restore the state of the random number generator to s, and then create a new random number. The value is the same as before.

rng(s); r1 = random('Poisson',5)
r1 = 5

Create a matrix of random numbers with the same size as an existing array. Use the stable distribution with shape parameters 2 and 0, scale parameter 1, and location parameter 0.

A = [3 2; -2 1]; sz = size(A); R = random('Stable',2,0,1,0,sz)
R =2×20.7604 -3.1945 2.5935 1.2193

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

R = random('Stable',2,0,1,0,size(A))
R =2×20.4508 -0.6132 -1.8494 0.4845

Create a Weibull probability distribution object using the default parameter values.

pd = makedist('Weibull')
pd = WeibullDistribution Weibull distribution A = 1 B = 1

Generate random numbers from the distribution.

rng('default')% For reproducibilityr = random(pd,10000,1);

Construct a histogram using 100 bins with a Weibull distribution fit.

histfit(r,100,'weibull')

Create a standard normal probability distribution object.

pd = makedist('Normal')
pd = NormalDistribution Normal distribution mu = 0 sigma = 1

Generate a 2-by-3-by-2 array of random numbers from the distribution.

r = random(pd,[2,3,2])
r = r(:,:,1) = 0.5377 -2.2588 0.3188 1.8339 0.8622 -1.3077 r(:,:,2) = -0.4336 3.5784 -1.3499 0.3426 2.7694 3.0349

Input Arguments

collapse all

Probability distribution name, specified as one of the probability distribution names in this table.

name Distribution Input Parameter A Input Parameter B Input Parameter C Input Parameter D
'Beta' Beta Distribution a: first shape parameter b: second shape parameter
'Binomial' Binomial Distribution n: number of trials p: probability of success for each trial
'BirnbaumSaunders' Birnbaum-Saunders Distribution β: scale parameter γ: shape parameter
'Burr' Burr Type XII Distribution α: scale parameter c: first shape parameter k: second shape parameter
'Chisquare' Chi-Square Distribution ν: degrees of freedom
'Exponential' Exponential Distribution μ: mean
'Extreme Value' Extreme Value Distribution μ: location parameter σ: scale parameter
'F' F Distribution ν1: numerator degrees of freedom ν2: denominator degrees of freedom
'Gamma' Gamma Distribution a: shape parameter b: scale parameter
'Generalized Extreme Value' Generalized Extreme Value Distribution k: shape parameter σ: scale parameter μ: location parameter
'Generalized Pareto' Generalized Pareto Distribution k: tail index (shape) parameter σ: scale parameter μ: threshold (location) parameter
'Geometric' Geometric Distribution p: probability parameter
'HalfNormal' Half-Normal Distribution μ: location parameter σ: scale parameter
'Hypergeometric' Hypergeometric Distribution m: size of the population k: number of items with the desired characteristic in the population n: number of samples drawn
'InverseGaussian' Inverse Gaussian Distribution μ: scale parameter λ: shape parameter
'Logistic' Logistic Distribution μ: mean σ: scale parameter
'LogLogistic' Loglogistic Distribution μ: mean of logarithmic values σ: scale parameter of logarithmic values
'Lognormal' Lognormal Distribution μ: mean of logarithmic values σ: standard deviation of logarithmic values
'Nakagami' Nakagami Distribution μ: shape parameter ω: scale parameter
'Negative Binomial' Negative Binomial Distribution r: number of successes p: probability of success in a single trial
'Noncentral F' Noncentral F Distribution ν1: numerator degrees of freedom ν2: denominator degrees of freedom δ:非中心参数
'Noncentral t' Noncentral t Distribution ν: degrees of freedom δ:非中心参数
'Noncentral Chi-square' Noncentral Chi-Square Distribution ν: degrees of freedom δ:非中心参数
'Normal' Normal Distribution μ: mean σ: standard deviation
'Poisson' Poisson Distribution λ: mean
'Rayleigh' Rayleigh Distribution b: scale parameter
'Rician' Rician Distribution s:非中心参数 σ: scale parameter
'Stable' Stable Distribution α: first shape parameter β: second shape parameter γ: scale parameter δ: location parameter
'T' Student's t Distribution ν: degrees of freedom
'tLocationScale' t Location-Scale Distribution μ: location parameter σ: scale parameter ν: shape parameter
'Uniform' Uniform Distribution (Continuous) a: lower endpoint (minimum) b: upper endpoint (maximum)
'Discrete Uniform' Uniform Distribution (Discrete) n: maximum observable value
'Weibull' Weibull Distribution a: scale parameter b: shape parameter

Example:'Normal'

First probability distribution parameter, specified as a scalar value or an array of scalar values.

If one or more of the input argumentsA,B,C, andDare arrays, then the array sizes must be the same. In this case,randomexpands each scalar input into a constant array of the same size as the array inputs.

Data Types:single|double

Second probability distribution parameter, specified as a scalar value or an array of scalar values.

If one or more of the input argumentsA,B,C, andDare arrays, then the array sizes must be the same. In this case,randomexpands each scalar input into a constant array of the same size as the array inputs.

Data Types:single|double

Third probability distribution parameter, specified as a scalar value or an array of scalar values.

If one or more of the input argumentsA,B,C, andDare arrays, then the array sizes must be the same. In this case,randomexpands each scalar input into a constant array of the same size as the array inputs.

Data Types:single|double

Fourth probability distribution parameter, specified as a scalar value or an array of scalar values.

If one or more of the input argumentsA,B,C, andDare arrays, then the array sizes must be the same. In this case,randomexpands each scalar input into a constant array of the same size as the array inputs.

Data Types:single|double

Probability distribution, specified as a probability distribution object created with a function or app in this table.

Function or App Description
makedist Create a probability distribution object using specified parameter values.
fitdist Fit a probability distribution object to sample data.
Distribution Fitter Fit a probability distribution to sample data using the interactive Distribution Fitter app and export the fitted object to the workspace.
paretotails Create a piecewise distribution object that has generalized Pareto distributions in the tails.

Size of each dimension, specified as integer values or a row vector of integer values. For example, specifying5,3,2or(5、3、2)generates a 5-by-3-by-2 array of random numbers from the specified probability distribution.

If one or more of the input argumentsA,B,C, andDare arrays, then the specified dimensionssz1,...,szNmust match the common dimensions ofA,B,C, andDafter any necessary scalar expansion. The default values ofsz1,...,szNare the common dimensions.

  • If you specify a single valuesz1, thenRis a square matrix of sizesz1.

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

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

Data Types:single|double

Output Arguments

collapse all

Random number generated from the specified probability distribution, returned as a scalar value or an array of scalar values with the dimensions specified bysz1,...,szN.

If you specify distribution parametersA,B,C, orD, then each element inRis the random number generated from the distribution specified by the corresponding elements inA,B,C, andD.

Alternative Functionality

  • randomis a generic function that accepts either a distribution by its name'name'或probability distribution objectpd. It is faster to use a distribution-specific function, such asrandnandnormrndfor the normal distribution andbinorndfor the binomial distribution. For a list of distribution-specific functions, seeSupported Distributions.

  • 使用随机number generation user interfacerandtoolto generate random numbers interactively.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?