Main Content

betarnd

Beta random numbers

Description

R = betarnd(A,B) generates random numbers R from the beta distributions with shape parameters A and B.

example

R = betarnd(A,B,sz1,...,szN) additionally specifies the dimensions of R using separate input arguments.

example

R = betarnd(A,B,sz) specifies the dimensions of R using a size vector sz.

example

Examples

collapse all

Specify the shape parameters.

rng(0,"twister") %  For reproducibility
A = randi(10,2,3);
B = 10+randi(20,2,3);

Generate random numbers from beta distributions

R = betarnd(A,B)
R = 2×3

    0.2965    0.1475    0.2692
    0.3189    0.3826    0.0746

Generate an array of random numbers from the beta distribution with shape parameters 5 and 10.

rng(0,"twister") %  For reproducibility
betarnd(5,10,3,4,2)
ans = 
ans(:,:,1) =

    0.4986    0.3339    0.2388    0.1170
    0.5070    0.5211    0.4663    0.4289
    0.1346    0.3169    0.5284    0.3643


ans(:,:,2) =

    0.4053    0.2278    0.1724    0.1860
    0.2193    0.3547    0.3914    0.3751
    0.2229    0.2115    0.3497    0.6088

The output is a 3-by-4-by-2 array.

Input Arguments

collapse all

First shape parameter, specified as a positive scalar value or an array of positive scalar values. If either A or B is a scalar, then betarnd expands the scalar argument into a constant array of the same size as the other argument.

When you specify sz1,...,szN, A must be a scalar or an sz1-by-...-by-szN array.

Example: [0.75,0.5;10 100]

Data Types: single | double

Second shape parameter, specified as a positive scalar value or an array of positive scalar values. If either A or B is a scalar, then betarnd expands the scalar argument into a constant array of the same size as the other argument.

When you specify sz1,...,szN, B must be a scalar or an sz1-by-...-by-szN array.

Example: [0.2,100; 4 7]

Data Types: single | double

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

If either A or B is an array, then the specified dimensions sz1,...,szN must match the common dimensions of A and B after any necessary scalar expansion. The default values of sz1,...,szN are the common dimensions.

  • If you specify a single value sz1, then R is a square matrix of size sz1-by-sz1.

  • If the size of any dimension is 0 or negative, then R is an empty array.

  • Beyond the second dimension, betarnd ignores trailing dimensions with a size of 1. For example, betarnd(A,B,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 integer values. Each element of this vector indicates the size of the corresponding dimension:

  • If the size of any dimension is 0, then R is an empty array.

  • If the size of any dimension is negative, then it is treated as 0.

  • Beyond the second dimension, betarnd ignores trailing dimensions with a size of 1.

Example: sz = [2 3 4] creates a 2-by-3-by-4 array.

Data Types: single | double

Output Arguments

collapse all

Random numbers from the beta distribution, returned as a scalar value or an array of scalar values.

Data Types: single | double

Alternative Functionality

  • betarnd is a function specific to beta distribution. Statistics and Machine Learning Toolbox™ also offers the generic function random, which supports various probability distributions. To use random, specify the probability distribution name and its parameters. Alternatively, create a BetaDistribution probability distribution object and pass the object as an input argument. Note that the distribution-specific function betarnd is faster than the generic function random.

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

Extended Capabilities

Version History

Introduced before R2006a