npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

distributions-normal-random

v0.0.1

Published

Generates normally distributed random variates.

Downloads

7

Readme

Normal Random Variables

NPM version Build Status Coverage Status Dependencies

Creates a matrix or array filled with draws from a normal distribution.

Installation

$ npm install distributions-normal-random

For use in the browser, use browserify.

Usage

var random = require( 'distributions-normal-random' );

random( [dims][, opts] )

Creates a matrix or array filled with draws from a normal distribution. The dims argument may either be a positive integer specifying a length or an array of positive integers specifying dimensions. If no dims argument is supplied,the function returns a single random draw from a normal distribution.

var out;

// Set seed
random.seed = 2;

out = random( 5 );
// returns [ ~-0.832, ~0.735, ~-1.432, ~0.057, -0.13 ]

out = random( [2,1,2] );
// returns [ [ [~0.969,~-0.394] ], [ [~0.599,~-1.511] ] ]

The function accepts the following options:

  • mu: mean parameter. Default: 0.
  • sigma: standard deviation. Default: 1.
  • seed: positive integer used as a seed to initialize the generator. If not supplied, uniformly distributed random numbers are generated via an underlying generator seedable by setting the seed property of the exported function.
  • dtype: output data type (see matrix for a list of acceptable data types). Default: generic.

The normal distribution is a function of two parameters: mu(mean) and sigma > 0(standard deviation). By default, mu is equal to 0 and sigma is equal to 1. To adjust either parameter, set the corresponding option.

var out = random( 5, {
	'mu': 20,
	'sigma': 4,
});
// returns [ ~25.293, ~21.105, ~21.347, 22.57, ~18.726 ]

To be able to reproduce the generated random numbers, set the seed option to a positive integer.

var out = random( 3, {
	'seed': 22
});
// returns [ ~-0.643, ~0.937, ~0.049 ]

var out = random( 3, {
    'seed': 22
});
// returns [ ~-0.643, ~0.937, ~0.049 ]

If no seed option is supplied, each function call uses a common underlying uniform number generator. A positive-integer seed for this underlying generator can be supplied by setting the seed property of the exported function.

random.seed = 11;
var out = random();
// returns ~-0.921

var out = random();
// returns ~0.389

random.seed = 11;
var out = random();
// returns ~-0.921

var out = random();
// returns ~0.389

By default, the output data structure is a generic array. To output a typed array or matrix, set the dtype option.

var out;

out = random( 5, {
	'dtype': 'float32'
});
// returns Float32Array( [~0.166,~0.916,~-0.003,-0.08,~-2.608] )

out = random( [3,2], {
	'dtype': 'float64'
});
/*
	[ ~-0.482 ~0.274
	  ~0.725 ~1.113
	  ~0.608 1.05 ]
*/

Notes:

  • Currently, for more than 2 dimensions, the function outputs a generic array and ignores any specified dtype.

    var out = random( [2,1,3], {
    	'dtype': 'float32'
    });
    // returns [ [ [~0.873,~-0.510,~-0.370] ], [ [~0.393,~-0.233,~0.907] ] ]
    

Method

The used algorithm to generate normal random variables is the "Improved Ziggurat method" developed by J. Doornik. In a speed comparison of different algorithms, Doornik found that the Ziggurat method was two or three times faster than the commonly used polar method (Box-Mueller Transform) when generating 10^9 standard normal random numbers.

Reference:

Doornik, J. a. (2005). An Improved Ziggurat Method to Generate Normal Random Samples.

Examples

var random = require( 'distributions-normal-random' ),
	out;

// Set seed
random.seed = 11;

// Plain arrays...

// 1x10:
out = random( 10 );

// 2x1x3:
out = random( [2,1,3] );

// 5x5x5:
out = random( [5,5,5] );

// 10x5x10x20:
out = random( [10,5,10,20] );

// Typed arrays...
out = random( 10, {
	'dtype': 'float32'
});

// Matrices...
out = random( [3,2], {
	'dtype': 'float64'
});

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

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:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015-2016. The Compute.io Authors.