# Negative Binomial Random Numbers

Negative binomially distributed pseudorandom numbers.

## Usage

``````var negativeBinomial = require( '@stdlib/random/base/negative-binomial' );
``````

#### negativeBinomial( r, p )

Returns a pseudorandom number drawn from a negative binomial distribution with parameters `r` (number of successes until experiment is stopped) and `p` (success probability).

``````var r = negativeBinomial( 20, 0.8 );
// returns <number>
``````

If `r` or `p` is `NaN`, the function returns `NaN`.

``````var r = negativeBinomial( NaN, 0.4 );
// returns NaN

r = negativeBinomial( 20, NaN );
// returns NaN
``````

If `p` is not in the interval `(0,1)` (exclusive), the function returns `NaN`.

``````var r = negativeBinomial( 10, 1.1 );
// returns NaN

r = negativeBinomial( 10, 1.0 );
// returns NaN

r = negativeBinomial( 10, 0.0 );
// returns NaN
``````

#### negativeBinomial.factory( [r, p, ][options] )

Returns a pseudorandom number generator (PRNG) for generating pseudorandom numbers drawn from a negative binomial distribution.

``````var rand = negativeBinomial.factory();

var r = rand( 20, 0.3 );
// returns <number>
``````

If provided `r` and `p`, the returned generator returns random variates from the specified distribution.

``````var rand = negativeBinomial.factory( 10, 0.8 );

var r = rand();
// returns <number>

r = rand();
// returns <number>
``````

If not provided `r` and `p`, the returned generator requires that both parameters be provided at each invocation.

``````var rand = negativeBinomial.factory();

var r = rand( 20, 0.8 );
// returns <number>

r = rand( 123, 0.21 );
// returns <number>
``````

The function accepts the following `options`:

• seed: pseudorandom number generator seed.

To seed a pseudorandom number generator, set the `seed` option.

``````var rand = negativeBinomial.factory({
'seed': 12345
});

var r = negativeBinomial( 8, 0.9 );
// returns <number>

rand = negativeBinomial.factory( 8, 0.9, {
'seed': 12345
});

r = rand();
// returns <number>
``````

#### negativeBinomial.NAME

The generator name.

``````var str = negativeBinomial.NAME;
// returns 'negative-binomial'
``````

#### negativeBinomial.PRNG

The underlying pseudorandom number generator.

``````var prng = negativeBinomial.PRNG;
// returns <Function>
``````

#### negativeBinomial.SEED

The value used to seed `negativeBinomial()`.

``````var rand;
var r;
var i;

// Generate pseudorandom values...
for ( i = 0; i < 100; i++ ) {
r = negativeBinomial( 20, 0.5 );
}

// Generate the same pseudorandom values...
rand = negativeBinomial.factory( 20, 0.5, {
'seed': negativeBinomial.SEED
});
for ( i = 0; i < 100; i++ ) {
r = rand();
}
``````

## Notes

• The `r` parameter is the shape parameter of the gamma mixing distribution when expressing the negative binomial distribution as a Gammaâ€“Poisson mixture. Accordingly, `r` is not required to be an integer value.

## Examples

``````var negativeBinomial = require( '@stdlib/random/base/negative-binomial' );

var seed;
var rand;
var i;

// Generate pseudorandom numbers...
console.log( '\nseed: %d', negativeBinomial.SEED );
for ( i = 0; i < 100; i++ ) {
console.log( negativeBinomial( 10, 0.5 ) );
}

// Create a new pseudorandom number generator...
seed = 1234;
rand = negativeBinomial.factory( 8, 0.8, {
'seed': seed
});
console.log( '\nseed: %d', seed );
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}

// Create another pseudorandom number generator using a previous seed...
rand = negativeBinomial.factory( 10, 0.5, {
'seed': negativeBinomial.SEED
});
console.log( '\nseed: %d', negativeBinomial.SEED );
for ( i = 0; i < 100; i++ ) {
console.log( rand() );
}
``````