Uniform Random Numbers

Uniformly distributed pseudorandom numbers.

Usage

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

uniform( a, b )

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

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

If either a or b is NaN or a >= b, the function returns NaN.

var r = uniform( 2.0, 1.0 );
// returns NaN

r = uniform( NaN, 1.0 );
// returns NaN

r = uniform( 1.0, NaN );
// returns NaN

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

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

var rand = uniform.factory();

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

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

// Draw from uniform( -2.0, 2.0 ) distribution:
var rand = uniform.factory( -2.0, 2.0 );

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 = uniform.factory();

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

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

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

rand = uniform.factory( 2.0, 4.0, {
    'seed': 12345
});

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

uniform.NAME

The generator name.

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

uniform.PRNG

The underlying pseudorandom number generator.

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

uniform.SEED

The value used to seed uniform().

var rand;
var r;
var i;

// Generate pseudorandom values...
for ( i = 0; i < 100; i++ ) {
    r = uniform( 0.0, 10.0 );
}

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

Examples

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

var seed;
var rand;
var i;

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

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