Discrete Uniform Random Numbers

Discrete uniform distributed pseudorandom numbers.

Usage

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );

discreteUniform( a, b )

Returns a pseudorandom number drawn from a discrete uniform distribution with parameters a (minimum support) and b (maximum support).

var r = discreteUniform( 2, 50 );
// returns <number>

If either a or b is not an integer value or a > b, the function returns NaN.

var r = discreteUniform( 20, 10 );
// returns NaN

r = discreteUniform( NaN, 10 );
// returns NaN

r = discreteUniform( 10, NaN );
// returns NaN

discreteUniform.factory( [a, b, ][options] )

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

var rand = discreteUniform.factory();

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

If provided a and b, the returned generator returns random variates from the specified distribution.

// Draw from discreteUniform( -20, 20 ) distribution:
var rand = discreteUniform.factory( -20, 20 );

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

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

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

var rand = discreteUniform.factory();

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

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

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

rand = discreteUniform.factory( 20, 40, {
    'seed': 12345
});

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

discreteUniform.NAME

The generator name.

var str = discreteUniform.NAME;
// returns 'discrete-uniform'

discreteUniform.PRNG

The underlying pseudorandom number generator.

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

discreteUniform.SEED

The value used to seed discreteUniform().

var rand;
var r;
var i;

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

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

Examples

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );

var seed;
var rand;
var i;

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

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