Pseudorandom Number Generator Iterators

Pseudorandom number generator (PRNG) iterators.

Usage

var ns = require( '@stdlib/random/iter' );

ns

Namespace containing pseudorandom number generator (PRNG) iterators.

var iterators = ns;
// returns {...}

The namespace contains the following functions for creating iterator protocol-compliant iterators:

Examples

var roundn = require( '@stdlib/math/base/special/roundn' );
var mean = require( '@stdlib/stats/base/mean' );
var iter = require( '@stdlib/random/iter' );

var initialPrice = 100.0;
var currentPrice = initialPrice;
var numDays = 30;
var volatility = 0.02; // 2% daily volatility

// Create iterator for random price movements:
var priceIter = iter.normal( 0.0, volatility );
var prices = [ initialPrice ];
var dailyReturns = [];

// Simulate price movements:
var change;
var i;
for ( i = 0; i < numDays; i++ ) {
    change = priceIter.next().value;
    currentPrice *= ( 1.0 + change );
    prices.push( roundn( currentPrice, -2 ) );
    dailyReturns.push( change * 100.0 );
}

// Calculate summary statistics:
var totalReturn = ( ( currentPrice - initialPrice ) / initialPrice ) * 100.0;
var avgReturn = mean( numDays, dailyReturns, 1 );

// Print results:
console.log( 'Stock Price Simulation Results:' );
console.log( '-------------------------------' );
console.log( 'Initial Price: $%d', initialPrice );
console.log( 'Final Price: $%d', roundn( currentPrice, -2 ) );
console.log( 'Total Return: %d%', roundn( totalReturn, -2 ) );
console.log( 'Average Daily Return: %d%', roundn( avgReturn, -2 ) );
console.log( '\nPrice History:' );
console.log( prices.join( ' → ' ) );
Did you find this page helpful?