Main Content

binornd

Random numbers from binomial distribution

Description

r = binornd(n,p) generates random numbers from the binomial distribution specified by the number of trials n and the probability of success for each trial p.

n and p can be vectors, matrices, or multidimensional arrays of the same size. Alternatively, one or more arguments can be scalars. The binornd function expands scalar inputs to constant arrays with the same dimensions as the other inputs. The function returns a vector, matrix, or multidimensional array r of the same size as n and p.

example

r = binornd(n,p,sz1,...,szN) generates an array of random numbers from the binomial distribution with the scalar parameters n and p, where sz1,...,szN indicates the size of each dimension.

example

r = binornd(n,p,sz) generates an array of random numbers from the binomial distribution with the scalar parameters n and p, where vector sz specifies size(r).

example

Examples

collapse all

Generate an array of random numbers from the binomial distributions. For each distribution, you specify the number of trials and the probability of success for each trial.

Specify the numbers of trials.

n = 10:10:60
n = 1×6

    10    20    30    40    50    60

Specify the probabilities of success for each trial.

p = 1./n
p = 1×6

    0.1000    0.0500    0.0333    0.0250    0.0200    0.0167

Generate random numbers from the binomial distributions.

r = binornd(n,p)
r = 1×6

     0     1     1     0     1     1

Generate an array of random numbers from one binomial distribution. Here, the distribution parameters n and p are scalars.

Use the binornd function to generate random numbers from the binomial distribution with 100 trials, where the probability of success in each trial is 0.2. The function returns one number.

r_scalar = binornd(100,0.2)
r_scalar = 
20

Generate a 2-by-3 array of random numbers from the same distribution by specifying the required array dimensions.

r_array = binornd(100,0.2,2,3)
r_array = 2×3

    18    23    20
    18    24    23

Alternatively, specify the required array dimensions as a vector.

r_array = binornd(100,0.2,[2 3])
r_array = 2×3

    21    21    20
    26    18    23

Input Arguments

collapse all

Number of trials, specified as a positive integer or an array of positive integers.

Example: [10 20 50 100]

Data Types: single | double

Probability of success for each trial, specified as a scalar value or an array of scalar values. All values of p must belong to the interval [0 1].

Example: [0.01 0.1 0.5 0.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 binomial probability distribution.

If either n or p is an array, then the specified dimensions sz1,...,szN must match the common dimensions of n and p 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, binornd ignores trailing dimensions with a size of 1. For example, binornd(n,p,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 binomial probability distribution.

If either n or p is an array, then the specified dimensions sz must match the common dimensions of n and p after any necessary scalar expansion. The default values of sz 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, binornd ignores trailing dimensions with a size of 1. For example, binornd(n,p,[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

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

Data Types: single | double

Alternative Functionality

  • binornd is a function specific to binomial 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 BinomialDistribution probability distribution object and pass the object as an input argument. Note that the distribution-specific function binornd 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