Main Content

geornd

Geometric random numbers

Description

r = geornd(p) generates a random number from the geometric distribution with the probability parameter p.

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

example

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

The geometric distribution is useful for modeling the number of failures before one success in a series of independent trials, where each trial results in either success or failure, and the probability of success in any individual trial is the constant p.

Examples

collapse all

Generate a single random number from a geometric distribution with a probability parameter p equal to 0.01.

rng default  % For reproducibility
p = 0.01;
r1 = geornd(0.01)
r1 = 
20

The returned random number represents a single experiment in which 20 failures were observed before a success, where each independent trial has a probability of success p equal to 0.01.

Generate a 1-by-5 array of random numbers from a geometric distribution with a probability parameter p equal to 0.01.

r2 = geornd(p,1,5)
r2 = 1×5

     9   205     9    45   231

Each random number in the returned array represents the result of an experiment to determine the number of failures observed before a success, where each independent trial has a probability of success p equal to 0.01.

Generate a 1-by-3 array containing one random number from each of the three geometric distributions corresponding to the parameters in the 1-by-3 array of probabilities p.

p = [0.01 0.1 0.5];
r3 = geornd(p,[1 3])
r3 = 1×3

   127     5     0

Each element of the returned 1-by-3 array r3 contains one random number generated from the geometric distribution described by the corresponding parameter in p. The first element in r3 represents an experiment in which 127 failures were observed before a success, where each independent trial has a probability of success p equal to 0.01. The second element in r3 represents an experiment in which 5 failures were observed before a success, where each independent trial has a probability of success p equal to 0.1. The third element in r3 represents an experiment in which 0 failures were observed before a success—in other words, the first attempt was a success, where each independent trial has a probability of success p equal to 0.5.

Input Arguments

collapse all

Probability of success in a single trial, specified as a scalar or an array of scalars in the range [0,1]. To generate random numbers from multiple distributions, specify p using an array.

Data Types: single | double

Size of each dimension, specified as separate arguments of integers.

If p is an array, then the specified dimensions sz1,...,szN must match the dimensions of p. The default values of sz1,...,szN are the dimensions of p.

  • 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, geornd ignores trailing dimensions with a size of 1. For example, geornd(5,3,1,1,1) produces a 3-by-1 vector of random numbers from the distribution with five degrees of freedom.

Data Types: single | double

Size of each dimension, specified as a row vector of integers.

If p is an array, then the specified dimensions sz must match the dimensions of p. The default values of sz are the dimensions of p.

  • 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, geornd ignores trailing dimensions with a size of 1. For example, geornd(5,[3 1 1 1]) produces a 3-by-1 vector of random numbers from the distribution with five degrees of freedom.

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 r is the random number generated from the distribution specified by the corresponding element in p.

Alternative Functionality

  • geornd is a function specific to the geometric 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 geornd 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