Documentation Center

  • Trial Software
  • Product Updates

random

Class: prob.TruncatableDistribution
Package: prob

Generate random numbers from probability distribution object

Syntax

r = random(pd)
r = random(pd,sz1,...,szN)
r = random(pd,[sz1,...,szN])

Description

r = random(pd) generates a random number r from the probability distribution pd.

r = random(pd,sz1,...,szN) generates a sz1-by-...-by-szN array of random numbers from the probability distribution pd.

r = random(pd,[sz1,...,szN]) generates a sz1-by-...-by-szN array of random numbers from the probability distribution pd.

Input Arguments

expand all

pd — Probability distributionprobability distribution object

Probability distribution, specified as a probability distribution object. Create a probability distribution object with specified parameter values using makedist. Alternatively, for fittable distributions, create a probability distribution object by fitting it to data using fitdist or the Distribution Fitting app.

sz1,...,szN — Size of each dimensiontwo or more integer values | vector of integer values

Size of each dimension, specified as two or more integer values, or a vector of such values. For example, specifying 5,3,2 or [5,3,2] generates a 5-by-3-by-2 array of random numbers from the probability distribution pd.

Data Types: single | double

Output Arguments

expand all

r — Random numberscalar value | array of values

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

Examples

expand all

Generate One Random Number

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.

r = random(pd)
r =

    0.5377

Generate Multiple Random Numbers

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 distribution and visualize with a histogram.

r = random(pd,10000,1);
hist(r,100)

Generate a Multidimensional Array of Random Numbers

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])
random_make_array(:,:,1) =

   -1.0689   -2.9443    0.3252
   -0.8095    1.4384   -0.7549


random_make_array(:,:,2) =

    1.3703   -0.1022    0.3192
   -1.7115   -0.2414    0.3129

See Also

| |

Was this topic helpful?