randu

Uniformly distributed pseudorandom numbers between 0 and 1.

Usage

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

randu()

Returns a uniformly distributed pseudorandom number on the interval [0,1).

var v = randu();
// returns <number>

randu.factory( [options] )

Returns a pseudorandom number generator (PRNG) for generating uniformly distributed random numbers on the interval [0,1).

var rand = randu.factory();

The function accepts the following options:

  • name: name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers. Default: 'minstd-shuffle'.
  • seed: pseudorandom number generator seed.

By default, the underlying pseudorandom number generator is minstd-shuffle. To use a different PRNG, set the name option.

var rand = randu.factory({
    'name': 'minstd'
});

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

To seed a pseudorandom number generator, set the seed option.

var rand = randu.factory({
    'seed': 12345
});

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

randu.NAME

The generator name.

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

randu.PRNG

The underlying pseudorandom number generator.

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

randu.SEED

The value used to seed randu().

var rand;
var v;
var i;

// Generate pseudorandom values...
for ( i = 0; i < 100; i++ ) {
    v = randu();
}

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

randu.MIN

Minimum value lower bound.

var min = randu.MIN;
// returns 0.0

randu.MAX

Maximum value upper bound (not greater than 1 exclusive).

var max = randu.MAX;
// returns <number>

Notes

  • Warning: the default underlying source of pseudorandom numbers may change in the future. If exact reproducibility is required, either explicitly specify a PRNG via the name option or use an underlying PRNG directly.

Examples

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

var seed;
var rand;
var i;

// Generate pseudorandom numbers...
for ( i = 0; i < 100; i++ ) {
    console.log( randu() );
}

// Create a new pseudorandom number generator...
seed = 1234;
rand = randu.factory({
    'seed': seed
});
for ( i = 0; i < 100; i++ ) {
    console.log( rand() );
}

// Create another pseudorandom number generator using a previous seed...
rand = randu.factory({
    'seed': randu.SEED
});
for ( i = 0; i < 100; i++ ) {
    console.log( rand() );
}