random
Random numbers
Syntax
Description
generates an array of random numbers from the specified probability distribution
using input arguments from any of the previous syntaxes, where
R = random(___,sz1,...,szN)sz1,...,szN indicates the size of each
dimension.
Examples
Generate one random number from the normal distribution with the mean equal to 1 and the standard deviation equal to 5. Specify the distribution name 'Normal' and the distribution parameters.
rng('default') % For reproducibility mu = 1; sigma = 5; r = random('Normal',mu,sigma)
r = 3.6883
Create a normal distribution object and generate one random number using the object.
Create a normal distribution object with the mean equal to 1 and the standard deviation equal to 5.
mu = 1; sigma = 5; pd = makedist('Normal','mu',mu,'sigma',sigma);
Generate one random number from the distribution.
rng('default') % For reproducibility r = random(pd)
r = 3.6883
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×2
0.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×2
0.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 reproducibility r = 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
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 | N/A | N/A |
'Binomial' | Binomial Distribution | n number of trials | p probability of success for each trial | N/A | N/A |
'BirnbaumSaunders' | Birnbaum-Saunders Distribution | β scale parameter | γ shape parameter | N/A | N/A |
'Burr' | Burr Type XII Distribution | α scale parameter | c first shape parameter | k second shape parameter | N/A |
'Chisquare' or
'chi2' | Chi-Square Distribution | ν degrees of freedom | N/A | N/A | N/A |
'Exponential' | Exponential Distribution | μ mean | N/A | N/A | N/A |
'Extreme Value' or
'ev' | Extreme Value Distribution | μ location parameter | σ scale parameter | N/A | N/A |
'F' | F Distribution | ν1 numerator degrees of freedom | ν2 denominator degrees of freedom | N/A | N/A |
'Gamma' | Gamma Distribution | a shape parameter | b scale parameter | N/A | N/A |
'Generalized Extreme
Value' or 'gev' | Generalized Extreme Value Distribution | k shape parameter | σ scale parameter | μ location parameter | N/A |
'Generalized Pareto' or
'gp' | Generalized Pareto Distribution | k tail index (shape) parameter | σ scale parameter | μ threshold (location) parameter | N/A |
'Geometric' | Geometric Distribution | p probability parameter | N/A | N/A | N/A |
'Half Normal' or
'hn' | Half-Normal Distribution | μ location parameter | σ scale parameter | N/A | N/A |
'Hypergeometric' or
'hyge' | Hypergeometric Distribution | m size of the population | k number of items with the desired characteristic in the population | n number of samples drawn | N/A |
'InverseGaussian' | Inverse Gaussian Distribution | μ scale parameter | λ shape parameter | N/A | N/A |
'Logistic' | Logistic Distribution | μ mean | σ scale parameter | N/A | N/A |
'LogLogistic' | Loglogistic Distribution | μ mean of logarithmic values | σ scale parameter of logarithmic values | N/A | N/A |
'LogNormal' | Lognormal Distribution | μ mean of logarithmic values | σ standard deviation of logarithmic values | N/A | N/A |
'Loguniform' | Loguniform Distribution | a lower endpoint (minimum) | b upper endpoint (maximum) | N/A | N/A |
'Nakagami' | Nakagami Distribution | μ shape parameter | ω scale parameter | N/A | N/A |
'Negative Binomial' or
'nbin' | Negative Binomial Distribution | r number of successes | p probability of success in a single trial | N/A | N/A |
'Noncentral F' or
'ncf' | Noncentral F Distribution | ν1 numerator degrees of freedom | ν2 denominator degrees of freedom | δ noncentrality parameter | N/A |
'Noncentral t' or
'nct' | Noncentral t Distribution | ν degrees of freedom | δ noncentrality parameter | N/A | N/A |
'Noncentral Chi-square' or
'ncx2' | Noncentral Chi-Square Distribution | ν degrees of freedom | δ noncentrality parameter | N/A | N/A |
'Normal' | Normal Distribution | μ mean | σ standard deviation | N/A | N/A |
'Pearson' | Pearson Distribution | μ mean | σ standard deviation | γ skewness | κ kurtosis |
'Poisson' | Poisson Distribution | λ mean | N/A | N/A | N/A |
'Rayleigh' | Rayleigh Distribution | b scale parameter | N/A | N/A | N/A |
'Rician' | Rician Distribution | s noncentrality parameter | σ scale parameter | N/A | N/A |
'Stable' | Stable Distribution | α first shape parameter | β second shape parameter | γ scale parameter | δ location parameter |
'T' | Student's t Distribution | ν degrees of freedom | N/A | N/A | N/A |
'tLocationScale' | t Location-Scale Distribution | μ location parameter | σ scale parameter | ν shape parameter | N/A |
'Uniform' | Uniform Distribution (Continuous) | a lower endpoint (minimum) | b upper endpoint (maximum) | N/A | N/A |
'Discrete Uniform' or
'unid' | Uniform Distribution (Discrete) | n maximum observable value | N/A | N/A | N/A |
'Weibull' or
'wbl' | Weibull Distribution | a scale parameter | b shape parameter | N/A | N/A |
Example: 'Normal'
First probability distribution parameter, specified as a scalar value or an array of scalar values.
If one or more of the input arguments A,
B, C, and D are arrays, then
the array sizes must be the same. In this case, random expands each
scalar input into a constant array of the same size as the array inputs. See
name for the definitions of A,
B, C, and D for each
distribution.
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 arguments A,
B, C, and D are arrays, then
the array sizes must be the same. In this case, random expands each
scalar input into a constant array of the same size as the array inputs. See
name for the definitions of A,
B, C, and D for each
distribution.
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 arguments A,
B, C, and D are arrays, then
the array sizes must be the same. In this case, random expands each
scalar input into a constant array of the same size as the array inputs. See
name for the definitions of A,
B, C, and D for each
distribution.
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 arguments A,
B, C, and D are arrays, then
the array sizes must be the same. In this case, random expands each
scalar input into a constant array of the same size as the array inputs. See
name for the definitions of A,
B, C, and D for each
distribution.
Data Types: single | double
Probability distribution, specified as one of the probability distribution objects in this table.
| Distribution Object | Function or App to Create Probability Distribution Object |
|---|---|
BetaDistribution | makedist, fitdist, Distribution Fitter |
BinomialDistribution | makedist, fitdist,
Distribution Fitter |
BirnbaumSaundersDistribution | makedist, fitdist,
Distribution Fitter |
BurrDistribution | makedist, fitdist,
Distribution Fitter |
EmpiricalDistribution | fitdist |
ExponentialDistribution | makedist, fitdist,
Distribution Fitter |
ExtremeValueDistribution | makedist, fitdist,
Distribution Fitter |
GammaDistribution | makedist, fitdist,
Distribution Fitter |
GeneralizedExtremeValueDistribution | makedist, fitdist,
Distribution Fitter |
GeneralizedParetoDistribution | makedist, fitdist,
Distribution Fitter |
HalfNormalDistribution | makedist, fitdist,
Distribution Fitter |
InverseGaussianDistribution | makedist, fitdist,
Distribution Fitter |
KernelDistribution | fitdist, Distribution Fitter |
LogisticDistribution | makedist, fitdist,
Distribution Fitter |
LoglogisticDistribution | makedist, fitdist,
Distribution Fitter |
LognormalDistribution | makedist, fitdist,
Distribution Fitter |
LoguniformDistribution | makedist |
MultinomialDistribution | makedist |
NakagamiDistribution | makedist, fitdist,
Distribution Fitter |
NegativeBinomialDistribution | makedist, fitdist,
Distribution Fitter |
NormalDistribution | makedist, fitdist,
Distribution Fitter |
PearsonDistribution | makedist |
| Piecewise distribution with generalized Pareto distributions in the tails | paretotails |
PiecewiseLinearDistribution | makedist |
PoissonDistribution | makedist, fitdist,
Distribution Fitter |
RayleighDistribution | makedist, fitdist,
Distribution Fitter |
RicianDistribution | makedist, fitdist,
Distribution Fitter |
StableDistribution | makedist, fitdist,
Distribution Fitter |
tLocationScaleDistribution | makedist, fitdist,
Distribution Fitter |
TriangularDistribution | makedist |
UniformDistribution | makedist |
WeibullDistribution | makedist, fitdist,
Distribution Fitter |
Size of each dimension, specified as integer values. For example,
specifying 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 arguments A,
B, C, and
D are arrays, then the specified dimensions
sz1,...,szN must match the common dimensions of
A, B, C,
and D after any necessary scalar expansion. The default
values of sz1,...,szN are the common dimensions.
If you specify a single value
sz1, thenRis a square matrix of sizesz1-by-sz1.If the size of any dimension is
0or negative, thenRis an empty array.Beyond the second dimension,
randomignores trailing dimensions with a size of 1. For example, specifying3,1,1,1produces 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 specified probability distribution.
If one or more of the input arguments A,
B, C, and
D are arrays, then the specified dimensions
sz must match the common dimensions of
A, B, C,
and D after any necessary scalar expansion. The default
values of sz are the common dimensions.
If you specify a single value
[sz1], thenRis a square matrix of sizesz1-by-sz1.If the size of any dimension is
0or negative, thenRis an empty array.Beyond the second dimension,
randomignores trailing dimensions with a size of 1. For example, specifying[3 1 1 1]produces a 3-by-1 vector of random numbers.
Example: [5 3 2]
Data Types: single | double
Output Arguments
Random number generated from the specified probability distribution,
returned as a scalar value or an array of scalar values with the dimensions
specified by sz1,...,szN or
sz.
If you specify distribution parameters A,
B, C, or
D, then each element in R is
the random number generated from the distribution specified by the
corresponding elements in A, B,
C, and D.
Alternative Functionality
randomis a generic function that accepts either a distribution by its namenameor a 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, see Supported Distributions.To generate random numbers interactively, use
randtool, a user interface for random number generation.
Extended Capabilities
Usage notes and limitations:
The input argument
namemust be a compile-time constant. For example, to use the normal distribution, includecoder.Constant('Normal')in the-argsvalue ofcodegen(MATLAB Coder).Code generation does not support the probability distribution object (
pd) input argument.
For more information on code generation, see Introduction to Code Generation for Statistics and Machine Learning Functions and Overview of Code Generation Using MATLAB Coder (MATLAB Coder).
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006aStarting in R2023b, random supports Pearson distributions.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)