A linear congruential pseudorandom number generator (lcg).
npm install compute-lcgLinear Congruential Generator
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]
> A linear congruential pseudorandom number generator (lcg).
`` bash`
$ npm install compute-lcg
For use in the browser, use browserify.
To use the module,
` javascript`
var lcg = require( 'compute-lcg' );
#### lcg( [seed] )
Returns a pseudorandom number generator.
` javascript`
var rand = lcg();
To seed the generator, provide a positive integer seed
` javascript`
var rand = lcg( 1234 );
#### rand( [n] )
Returns a pseudorandom floating-point number between 0 and 1.
` javascript`
var val = rand();
If provided a length n, the method returns an array of pseudorandom numbers.
` javascript`
var arr = rand( 10 );
// returns [...]
For a general lcg reference, see Wikipedia. Linear congruential generators use the following recurrence relation:
X_{n+1} = ( a*X_n + c ) mod m
In this implementation, the constants a, c, and m have the following values:
a = 16807
c = 0
m = 2^31 - 1 => 2147483647
The values for a, c, and m are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_. For the most part, this implementation follows _Numerical Recipes_.
The generator has a period of approximately 2.1e9 [[4]](#ref-numerical-recipes-2).
Lcg is fast and uses little memory. On the other hand, because the generator is a simple linear congruential generator, it has recognized shortcomings. By today's PRNG standards, its period, on the order of 2e9, is relatively short. More importantly, the "randomness quality" of its output is not of the best quality. These defects rule it out, for example, in Monte Carlo simulations and in cryptographic applications. For more on the advantages and disadvantages of LCGs see [[5]](#ref-wikipedia-2).
` javascript
var lcg = require( 'compute-lcg' );
// Create a new (unseeded) generator:
var rand = lcg();
// Generate some pseudorandom numbers...
for ( var i = 0; i < 10; i++ ) {
console.log( rand() );
}
// Create a new (seeded) generator:
rand = lcg( 1 );
for ( var j = 0; j < 10; j++ ) {
console.log( rand() );
}
// Create a new generator seeded with the same seed as the previous generator:
rand = lcg( 1 );
console.log( rand( 10 ).join( '\n' ) );
`
To run the example code from the top-level application directory,
` bash`
$ node ./examples/index.js
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
` bash`
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
` bash`
$ make test-cov
Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,
` bash``
$ make view-cov
#### Test Notes
Test data generated from the C code published in _Numerical Recipes_.
1. Wikipedia. Linear Congruential Generator
2. S.K. Park and K.W. Miller (1988). "Random Number Generators: Good Ones Are Hard To Find". Communications of the ACM 31 (10): 1192-1201.
3. William H. Press, et. al., _Numerical Recipes in C: The Art of Scientific Computing_, Section 7.1 "Uniform Deviates" (2d ed. 1992) (hereinafter _Numerical Recipes_).
4. _Numerical Recipes_, p. 279.
5. Wikipedia. Linear Congruential Generator.
---
Copyright © 2014. rgizz.
[npm-image]: http://img.shields.io/npm/v/compute-lcg.svg
[npm-url]: https://npmjs.org/package/compute-lcg
[travis-image]: http://img.shields.io/travis/compute-io/lcg/master.svg
[travis-url]: https://travis-ci.org/compute-io/lcg
[coveralls-image]: https://img.shields.io/coveralls/compute-io/lcg/master.svg
[coveralls-url]: https://coveralls.io/r/compute-io/lcg?branch=master
[dependencies-image]: http://img.shields.io/david/compute-io/lcg.svg
[dependencies-url]: https://david-dm.org/compute-io/lcg
[dev-dependencies-image]: http://img.shields.io/david/dev/compute-io/lcg.svg
[dev-dependencies-url]: https://david-dm.org/dev/compute-io/lcg
[github-issues-image]: http://img.shields.io/github/issues/compute-io/lcg.svg
[github-issues-url]: https://github.com/compute-io/lcg/issues