Hauptinhalt

nbinrnd

Negative binomial random numbers

Description

y = nbinrnd(r,p) generates a random number drawn from the negative binomial distribution with the corresponding number of successes r and the probability of success in a single trial p.

y = nbinrnd(r,p,sz1,...,szN) generates an array of random numbers, where sz1,...,szN indicates the size of each dimension.

example

y = nbinrnd(r,p,sz) generates an array of random numbers, where the vector sz specifies size(r).

The simplest application of the negative binomial is the case of successive random trials, each having a constant probability p of success. The number of extra trials you must perform in order to observe a given number r of successes has a negative binomial distribution. However, consistent with a more general interpretation of the negative binomial, nbinrnd allows r to be any positive value, including nonintegers.

Examples

collapse all

Generate 5000 random numbers from a negative binomial distribution with the corresponding number of successes r and probability of success per trial p.

rng(0,"twister") % For reproducibility
r = 6;
p = 0.1;
y = nbinrnd(r,p,[5000 1]);

Plot a histogram of the numbers.

histogram(y)

Figure contains an axes object. The axes object contains an object of type histogram.

Input Arguments

collapse all

Number of successes, specified as a positive scalar or an array of positive scalars.

To generate random numbers from multiple distributions, specify either r or p (or both) using arrays. If either r or p is an array, then the array sizes must be the same. In this case, nbinrnd expands the scalar input into a constant array of the same size as the array input.

Data Types: single | double

Probability of success, specified as a positive scalar or an array of positive scalars in the range (0,1].

To generate random numbers from multiple distributions, specify either r or p (or both) using arrays. If either r or p is an array, then the array sizes must be the same. In this case, nbinrnd expands the scalar input into a constant array of the same size as the array input.

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 probability distribution.

If either r or p is an array, then the specified dimensions sz1,...,szN must match the common dimensions of r 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 y is a square matrix of size sz1-by-sz1.

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

  • Beyond the second dimension, nbinrnd ignores trailing dimensions with a size of 1. For example, specifying 3,1,1,1 produces a 3-by-1 vector of random numbers.

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 probability distribution.

If either r or p is an array, then the specified dimensions sz must match the common dimensions of r and p after any necessary scalar expansion. The default values of sz are the common dimensions.

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

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

  • Beyond the second dimension, nbinrnd ignores trailing dimensions with a size of 1. For example, specifying [3,1,1,1] produces a 3-by-1 vector of random numbers.

Data Types: single | double

Output Arguments

collapse all

Random numbers, returned as a scalar value or an array of scalar values with the dimensions specified by sz1,...,szN or sz. Each element in y is the random number generated from the distribution specified by the corresponding elements in r and p.

Alternative Functionality

  • nbinrnd is a function specific to the negative 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. Note that the distribution-specific function nbinrnd is faster than the generic function random.

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

Extended Capabilities

expand all

Version History

Introduced before R2006a