randomGeometric

Generates pseudorandom numbers from a geometric distribution.

Synopsis

randomGeometric(nRandom, p)

Required Arguments

int nRandom (Input)
Number of random numbers to generate.
float p (Input)
Probability of success on each trial. Parameter p must be positive and less than 1.0.

Return Value

An integer array of length nRandom containing the random geometric deviates.

Description

Function randomGeometric generates pseudorandom numbers from a geometric distribution with parameter P, where P is the probability of getting a success on any trial. A geometric deviate can be interpreted as the number of trials until the first success (including the trial in which the first success is obtained). The probability function is

\[f(x) = P(1-P)^{x-1}\]

for \(x=1,2,\ldots\) and \(0<P<1\).

The geometric distribution as defined above has mean \(1/P\).

The i-th geometric deviate is generated as the smallest integer not less than \((\log(U_i))/(\log(1-P))\), where the \(U_i\) are independent uniform(0, 1) random numbers (see Knuth 1981).

The geometric distribution is often defined on 0, 1, 2, …, with mean \((1-P)/P\). Such deviates can be obtained by subtracting 1 from each element of ir (the returned vector of random deviates).

Example

In this example, randomGeometric generates five pseudorandom geometric deviates from a geometric distribution with parameter an equal to 0.3.

from numpy import *
from pyimsl.stat.randomGeometric import randomGeometric
from pyimsl.stat.randomSeedSet import randomSeedSet
from pyimsl.stat.writeMatrix import writeMatrix

n_random = 5
p = 0.3
randomSeedSet(123457)
ir = randomGeometric(n_random, p)
writeMatrix("Geometric(0.3) random deviates:", ir,
            noColLabels=True)

Output

 
                Geometric(0.3) random deviates:
          1            4            1            2            1