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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gaussian-rng

v1.0.6

Published

A Gaussian random number generator with mean, standard deviation, and skew control.

Downloads

26

Readme

Gaussian Random Number Generator

License Tests Coverage

Gaussian-RNG is a lightweight and flexible JavaScript/TypeScript library for generating random numbers that follow a Gaussian (normal) distribution. Unlike standard uniform random number generators, this package ensures that numbers are distributed around a specified mean, with a controlled spread (standard deviation) and an optional skew to shift probability density toward one side.

Important: Setting a nonzero skew value intentionally pulls the output away from a perfectly normal (Gaussian) distribution. Instead of simply adjusting the spread, skew will shift a percentage of values across the mean, altering the symmetry of the distribution.

This library can be useful in scientific simulations, statistical modeling, procedural content generation (e.g., gaming, terrain generation), AI randomness tuning, finance, and Monte Carlo simulations, where naturally occurring variations tend to follow a normal distribution rather than uniform randomness.

With support for bounded Gaussian distributions (ensuring values stay within a given range), custom mean and standard deviation settings, and skew adjustments, Gaussian-RNG provides fine-grained control over random number generation in applications requiring realistic randomness.

Installation

Install via npm or yarn:

npm install gaussian-rng

or

yarn add gaussian-rng

Example Usage

Generating a Basic Gaussian Random Number

import { gaussianRandom } from "gaussian-rng";

// Generate a normally distributed random number with mean 0 and stdDev 1
const randomValue = gaussianRandom({ mean: 0, stdDev: 1 });
console.log("Random Value:", randomValue);

Generating a Skewed Gaussian Random Number

import { gaussianRandom } from "gaussian-rng";

// Generate a random number with a positive skew (shifts some below-mean values above the mean)
const skewedValue = gaussianRandom({ mean: 50, stdDev: 10, skew: 0.5 });
console.log("Skewed Value:", skewedValue);

Generating a Bounded Gaussian Random Number

This would mimic rolling three 6-sided dice and summing the values of all three.

import { boundedGaussianRandom } from "gaussian-rng";

// Generate a random number within the range [3, 18] with default centered mean and standard deviation.
const boundedValue = boundedGaussianRandom({ min: 3, max: 18 });
console.log("Bounded Value:", boundedValue);

Features

  • Customizable Parameters: Specify mean and standard deviation to control the distribution.
  • Optional Skew: Adjust the skew to intentionally pull the distribution away from normal. Positive skew moves up to 25% of below-mean values above the mean; negative skew moves up to 25% of above-mean values below the mean.
  • Bounded Generation: Ensure that generated numbers fall within a specific range.
  • Easy Integration: Designed for use in both JavaScript and TypeScript projects.

Understanding Skew in the Gaussian Distribution

Skewness refers to the asymmetry in the probability distribution of a real-valued random variable. In an ideal normal (Gaussian) distribution, the skewness is zero, meaning the distribution is perfectly symmetric around the mean.

  • Positive skew (skew > 0): Moves up to 25% of below-mean values above the mean, making the right tail heftier.
  • Negative skew (skew < 0): Moves up to 25% of above-mean values below the mean, making the left tail heftier.

How We Apply Skewness in the Distribution

Instead of modifying tail steepness, our approach shifts a percentage of values across the mean while maintaining a natural-ish Gaussian shape. The function:

function gaussianRandom(mean, stdDev, skew) {
  let u1 = Math.random();
  let u2 = Math.random();
  let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2);
  let value = z0 * stdDev + mean;

  // Apply skew: Move a proportion of values across the mean
  let flipChance = Math.abs(skew) * 0.25; // Up to 25% shift
  if (skew > 0 && value < mean && Math.random() < flipChance) {
    value = mean + (mean - value);
  } else if (skew < 0 && value > mean && Math.random() < flipChance) {
    value = mean - (value - mean);
  }
  return value;
}

This method ensures that skew values range from -1 to +1, with proportional movement:

| Skew Value | Effect | | ---------- | ------------------------------------------ | | -1 | Moves 25% of above-mean values below | | -0.5 | Moves 12.5% of above-mean values below | | 0 | No change, normal Gaussian | | 0.5 | Moves 12.5% of below-mean values above | | 1 | Moves 25% of below-mean values above |

This keeps the Gaussian distribution mostly intact while shifting values appropriately.

Explanation of the Box-Muller Transform

The Box-Muller transform is a method for generating normally distributed random numbers from uniformly distributed random numbers (i.e., Math.random()).

How the Box-Muller Transform Works

  1. Generate two independent uniform random numbers u1 and u2 in the range [0,1]:

    let u1 = Math.random();
    let u2 = Math.random();
  2. Apply the Box-Muller formula:

    let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2);
    • The Math.log(u1) term ensures values cluster around zero (Gaussian property).
    • Math.cos(2πu2) provides uniform circular sampling, ensuring randomness.
  3. This transformation results in z0, which is a normally distributed number with:

    • Mean (μ) of 0
    • Standard deviation (σ) of 1
  4. Finally, to scale and shift the result to the desired mean and standard deviation, we use:

    return z0 * stdDev + mean;

    This ensures:

    • Values are centered at mean
    • The spread is controlled by stdDev

Why Use the Box-Muller Transform?

  • It's efficient: generates two Gaussian numbers per transformation.
  • Uses only basic arithmetic and trigonometry.
  • Works well in applications needing high performance.

Additional Example Use

Imagine we have a basketball player who is the 90th percentile for the league in shooting free throws, and we know that the league average is 70% for making free throw shots. From this, we can develop parameters and feed them to a Gaussian random number generator to create a realistic set of numbers for this player over multiple games.

const freeThrowPercentage = gaussianRandom({ mean: 88, stdDev: 5, skew: -0.5 });
console.log(
  `Simulated Free Throw Percentage: ${freeThrowPercentage.toFixed(2)}%`
);

This ensures a realistic variation in free throw percentages, allowing for occasional high and low performances while keeping the overall distribution accurate.

Visualization

If you would like to see what your distribution looks like and you have open installed globally for viewing html, you can clone the repository and

open html/gaussian-vis.html

The shown page will allow you see how values will be distributed based on your settings.