Laplace Random Numbers

Laplace (double exponential) distributed pseudorandom numbers.

Usage

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

laplace( mu, b )

Returns a pseudorandom number drawn from a Laplace (double exponential) distribution with parameters mu (mean) and b (scale parameter).

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

If mu or b is NaN or b <= 0, the function returns NaN.

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

r = laplace( NaN, 5.0 );
// returns NaN

r = laplace( 2.0, NaN );
// returns NaN

laplace.factory( [mu, b, ][options] )

Returns a pseudorandom number generator (PRNG) for generating pseudorandom numbers drawn from a Laplace (double exponential) distribution.

var rand = laplace.factory();

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

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

var rand = laplace.factory( 10.0, 2.0 );

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

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

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

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

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

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

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

laplace.NAME

The generator name.

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

laplace.PRNG

The underlying pseudorandom number generator.

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

laplace.SEED

The value used to seed laplace().

var rand;
var r;
var i;

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

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

Examples

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

var seed;
var rand;
var i;

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

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