Skip to content

Commit

Permalink
Refactor random & gaussian API
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 17, 2011
1 parent bcb526c commit 2da34e6
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 360 deletions.
9 changes: 2 additions & 7 deletions index.js
Expand Up @@ -2,19 +2,14 @@
var gsl = require('./build/default/gsl');

module.exports = {
Random: gsl.Random,
random: {
get: gsl.rngGet,
Get: gsl.RngGet,
min: gsl.rngMin,
max: gsl.rngMax,
uniform: gsl.rngUniform,
Uniform: gsl.RngUniform,
gaussian: gsl.ranGaussian,
Gaussian: gsl.RanGaussian,
gaussianZiggurat: gsl.ranGaussianZiggurat,
GaussianZiggurat: gsl.RanGaussianZiggurat,
gaussianRatioMethod: gsl.ranGaussianRatioMethod,
GaussianRatioMethod: gsl.RanGaussianRatioMethod
gaussianRatioMethod: gsl.ranGaussianRatioMethod
}
};

64 changes: 26 additions & 38 deletions readme.md
Expand Up @@ -29,82 +29,70 @@ Via git (or downloaded tarball):
Random API
----------

Seed arguments are always optional and must be provided as unsigned integers.
The library takes two forms, functions and iterator objects. Both respect the same names with different conventions. For exemple, obtaining an uniform random name can be done as `random.get()` as well as `(new Random).get()`. If you wished to provide a seed, then you'll respectivelly call `random.get(seed)` and `(new Random(seed)).get()`

Using an iterator objects make sense when performance is in consideration, specially when using seeds.

Seeds are always optional and must be provided as unsigned integers. Deviations, used by gaussian functions, are float and default to 3.

- *Random([seed])*
Construct a new iterator, seel below for available random methods.

### [Sampling](http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html)

- *random.get([seed])*
*Random.get()*
Returns a random integer. The minimum and maximum values depend on the algorithm used, but all integers in the range [min,max] are equally likely. The values of min and max can determined using the auxiliary functions `random.min()` and `random.max()`.
- *random.Get([seed])*
Creates an iterator. Take an optional seed argument as an unsigned int. Random number are obtained by calling `next`.
*random.Get.next()*
Same as `random.get` but called from a `random.Get` instance.

- *random.min()*
*Random.min()*
Returns the smallest value that `random.get()` can return

- *random.max()*
*Random.max()*
Returns the largest value that `random.get()` can return

- *random.uniform([seed])*
*Random.uniform([])*
Returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0.
- *random.Uniform([seed])*
Creates an iterator. Take an optional seed argument as an unsigned int. Random number are obtained by calling `next`.
*random.Uniform.next()*
Same as `random.uniform` but called from a `random.Uniform` instance.

### [Gaussian](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)

- *random.gaussian([seed], [deviation])*
*Random.gaussian([deviation])*
Returns a Gaussian random float with mean zero. Standart deviation is a float and default to 3.
- *random.Gaussian([seed])*
Creates an iterator. Take an optional seed argument as an unsigned int. Random number are obtained by calling `next` with an optional deviation.
Also, the `seed` property is accessible.
*random.Gaussian.next([deviation])*
Same as `random.gaussian` but called from a `random.Gaussian` instance.
*random.Gaussian.seed*
Returns the seed used by the iterator.

- *random.gaussianZiggurat([seed], [deviation])*
*Random.gaussianZiggurat([deviation])*
Same as `random.gaussian` but using the alternative Marsaglia-Tsang ziggurat method.
- *random.GaussianZiggurat([seed])*
Same as `random.Gaussian` but using the alternative Marsaglia-Tsang ziggurat method.

- *random.gaussianRatioMethod([seed], [deviation])*
*Random.gaussianRatioMethod([deviation])*
Same as `random.gaussian` but using the alternative Kinderman-Monahan-Leva ratio method.
- *random.GaussianRatioMethod([seed])*
Same as `random.Gaussian` but using the alternative Kinderman-Monahan-Leva ratio method.

Exemple

var random = require('gsl').random,
var gsl = require('gsl'),
seed = 50,
deviation = 0.5;

console.log( random.gaussian() );
console.log( random.gaussian(seed) );
console.log( random.gaussian(seed,deviation) );
console.log( gsl.random.gaussian() );
console.log( gsl.random.gaussian(seed) );
console.log( gsl.random.gaussian(seed, deviation) );

var it1 = new random.Gaussian();
console.log( it1.next() );
it1.seed;
var it1 = new gsl.Random();
console.log( it1.gaussian() );

var it1 = new random.Gaussian(seed);
console.log( it1.next(deviation) );
console.log( it1.seed );
var it1 = new gsl.Random(seed);
console.log( it1.gaussian(deviation) );

Running the tests
-----------------

Tests are executed with expresso. To install it, simple use `npm install expresso`.
Tests are executed with expresso. To install it, simply issue `npm install expresso`.

To run the tests
expresso test
expresso

Contributors
------------
Expand Down
20 changes: 9 additions & 11 deletions samples/gaussian.js
@@ -1,16 +1,14 @@

var random = require('gsl').random,
var gsl = require('gsl'),
seed = 50,
deviation = 0.5;

console.log( gsl.random.gaussian() );
console.log( gsl.random.gaussian(seed) );
console.log( gsl.random.gaussian(seed, deviation) );

console.log( random.gaussian() );
console.log( random.gaussian(seed) );
console.log( random.gaussian(seed,deviation) );

var it1 = new random.Gaussian();
console.log( it1.next() );
it1.seed;
var it1 = new gsl.Random();
console.log( it1.gaussian() );

var it1 = new random.Gaussian(seed);
console.log( it1.next(deviation) );
console.log( it1.seed );
var it1 = new gsl.Random(seed);
console.log( it1.gaussian(deviation) );
12 changes: 6 additions & 6 deletions samples/gaussian_speed.js
Expand Up @@ -54,10 +54,10 @@ var assert = require('assert'),
console.log('C++ fn',(new Date).getTime() - time);

var time = (new Date).getTime();
var random = new gsl.random.Gaussian(1978);
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
random.next();
random.gaussian();
}
}
console.log('C++ it',(new Date).getTime() - time);
Expand All @@ -71,10 +71,10 @@ var assert = require('assert'),
console.log('C++ fn Ziggurat',(new Date).getTime() - time);

var time = (new Date).getTime();
var random = new gsl.random.GaussianZiggurat(1978);
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
random.next();
random.gaussianZiggurat();
}
}
console.log('C++ it Ziggurat',(new Date).getTime() - time);
Expand All @@ -88,10 +88,10 @@ var assert = require('assert'),
console.log('C++ fn RatioMethod',(new Date).getTime() - time);

var time = (new Date).getTime();
var random = new gsl.random.GaussianRatioMethod(1978);
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
random.next();
random.gaussianRatioMethod();
}
}
console.log('C++ it RatioMethod',(new Date).getTime() - time);
Expand Down
8 changes: 4 additions & 4 deletions samples/random_speed.js
Expand Up @@ -39,10 +39,10 @@ var assert = require('assert'),
console.log('C++ get fn',(new Date).getTime() - time);

var time = (new Date).getTime();
var random = new gsl.random.Get(1978);
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
random.next();
random.get();
}
}
console.log('C++ get it',(new Date).getTime() - time);
Expand All @@ -56,10 +56,10 @@ var assert = require('assert'),
console.log('C++ uniform fn',(new Date).getTime() - time);

var time = (new Date).getTime();
var random = new gsl.random.Uniform(1978);
var random = new gsl.Random(1978);
for(var i=0;i<1000000;i++){
for(var j=0;j<2;j++){
random.next();
random.uniform();
}
}
console.log('C++ uniform it',(new Date).getTime() - time);
Expand Down

0 comments on commit 2da34e6

Please sign in to comment.