Beta Random Numbers

Beta distributed pseudorandom numbers.

Usage

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

beta( alpha, beta )

Returns a pseudorandom number drawn from a beta distribution with parameters `alpha` (first shape parameter) and `beta` (second shape parameter).

``````var r = beta( 2.0, 5.0 );
// returns <number>
``````

If `alpha <= 0` or `beta <= 0`, the function returns `NaN`.

``````var r = beta( 2.0, -2.0 );
// returns NaN

r = beta( -2.0, 2.0 );
// returns NaN
``````

If `alpha` or `beta` is `NaN`, the function returns `NaN`.

``````var r = beta( NaN, 5.0 );
// returns NaN

r = beta( 2.0, NaN );
// returns NaN
``````

beta.factory( [alpha, beta, ][options] )

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

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

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

If provided `alpha` and `beta`, the returned generator returns random variates from the specified distribution.

``````// Draw from beta( 1.5, 1.5 ) distribution:
var rand = beta.factory( 1.5, 1.5 );

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

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

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

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

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

r = rand( 3.14, 2.25 );
// 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 = beta.factory({
'seed': 12345
});

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

rand = beta.factory( 1.0, 2.0, {
'seed': 12345
});

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

beta.NAME

The generator name.

``````var str = beta.NAME;
// returns 'beta'
``````

beta.PRNG

The underlying pseudorandom number generator.

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

beta.SEED

The value used to seed `beta()`.

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

// Generate pseudorandom values...
for ( i = 0; i < 100; i++ ) {
r = beta( 2.0, 2.0 );
}

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

Examples

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

var seed;
var rand;
var i;

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

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

References

• Ahrens, J.H., and U. Dieter. 1974. "Computer methods for sampling from gamma, beta, poisson and bionomial distributions." Computing 12 (3): 223–46. doi:10.1007/BF02293108.
• Jöhnk, M.D. 1964. "Erzeugung von Betaverteilten Und Gammaverteilten Zufallszahlen." Metrika 8: 5–15. <http://eudml.org/doc/175224>.