Binomial Random Numbers

Binomial distributed pseudorandom numbers.

Usage

var binomial = require( '@stdlib/random/base/binomial' );

binomial( n, p )

Returns a pseudorandom number drawn from a binomial distribution with number of trials n and success probability p.

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

If n is not a positive integer or p is not a probability, the function returns NaN.

var r = binomial( 1.5, 0.5 );
// returns NaN

r = binomial( 2, 1.5 );
// returns NaN

If n or p is NaN, the function returns NaN.

var r = binomial( NaN, 0.4 );
// returns NaN

r = binomial( 20, NaN );
// returns NaN

binomial.factory( [n, p, ][options] )

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

var rand = binomial.factory();

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

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

// Draws from binomial( 10, 0.8 ):
var rand = binomial.factory( 10, 0.8 );

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

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

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

var rand = binomial.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 = binomial.factory({
    'seed': 12345
});

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

rand = binomial.factory( 100, 0.2, {
    'seed': 12345
});

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

binomial.NAME

The generator name.

var str = binomial.NAME;
// returns 'binomial'

binomial.PRNG

The underlying pseudorandom number generator.

var prng = binomial.PRNG;
// returns <Function>

binomial.SEED

The value used to seed binomial().

var rand;
var r;
var i;

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

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

Examples

var binomial = require( '@stdlib/random/base/binomial' );

var seed;
var rand;
var i;

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

// Create a new pseudorandom number generator...
seed = 1234;
rand = binomial.factory( 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 = binomial.factory( 0.5, {
    'seed': binomial.SEED
});
console.log( '\nseed: %d', binomial.SEED );
for ( i = 0; i < 100; i++ ) {
    console.log( rand() );
}

References

  • Hörmann, Wolfgang. 1993. "The generation of binomial random variates." Journal of Statistical Computation and Simulation 46 (1-2): 101–10. doi:10.1080/00949659308811496.