Triangular Random Numbers

Triangular distributed pseudorandom numbers.

Usage

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

triangular( a, b, c )

Returns a pseudorandom number drawn from a triangular distribution with parameters a (minimum support), b (maximum support), and c (mode).

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

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

var r = triangular( NaN, 1.0, 0.5 );
// returns NaN

r = triangular( 1.0, NaN, 2.42 );
// returns NaN

r = triangular( 1.0, 10.0, NaN );
// returns NaN

If the condition a <= c <= b is not satisfied, the function returns NaN.

var r = triangular( 13.0, 9.0, 0.5 );
// returns NaN

triangular.factory( [a, b, c, ][options] )

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

var rand = triangular.factory();

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

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

var rand = triangular.factory( -2.0, 2.0, 1.0 );

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

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

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

var rand = triangular.factory();

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

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

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

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

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

triangular.NAME

The generator name.

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

triangular.PRNG

The underlying pseudorandom number generator.

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

triangular.SEED

The value used to seed triangular().

var rand;
var r;
var i;

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

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

Examples

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

var seed;
var rand;
var i;

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

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