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

randlibjs

v1.0.22

Published

Module implementing pseudo-random number generators with the ability to draw samples from a variety of probability distributions.

Readme

Randlibjs

A lightweight JavaScript library for generating random numbers with various probability distributions.

Version License Build Status Codecov Maintenance FOSSA Status Downloads

Features

  • Multiple Distributions: Generate random numbers from various probability distributions including normal, uniform, exponential, and more
  • Reproducible Results: Set seeds to generate reproducible sequences of random numbers for testing and validation
  • Lightweight: Minimal dependencies and optimized code for fast performance in both browser and Node.js environments
  • Easy to Use: Simple and intuitive API with comprehensive documentation and examples

Installation

npm

npm install randlibjs

Quick Start

ES Modules

// Import specific functions
import { normal, uniform, seed } from 'randlibjs';

// Set a seed for reproducibility
seed(12345);

// Generate 10 numbers from normal distribution
const normalSample = normal(0, 1, 10);
console.log(normalSample);

// Generate 5 uniformly distributed numbers
const uniformSample = uniform(0, 10, 5);
console.log(uniformSample);

Browser

<script src="https://cdn.jsdelivr.net/npm/randlibjs@latest/dist/randlib.min.js"></script>
<script>
  // Set a seed
  randlib.seed(12345);
  
  // Generate random numbers
  const normalSample = randlib.normal(0, 1, 10);
  console.log(normalSample);
</script>

Available Distributions

Continuous Distributions

| Function | Description | Parameters | |----------|-------------|------------| | uniform(low, high, size) | Uniform distribution | low: Lower bound (default: 0)high: Upper bound (default: 1)size: Sample size (default: 1) | | normal(mean, std, size) | Normal (Gaussian) distribution | mean: Mean (default: 0)std: Standard deviation (default: 1)size: Sample size (default: 1) | | exponential(lambda, size) | Exponential distribution | lambda: Rate parameter (default: 1)size: Sample size (default: 1) | | cauchy(loc, scale, size) | Cauchy distribution | loc: Location parameter (default: 0)scale: Scale parameter (default: 1)size: Sample size (default: 1) | | lognormal(mean, sigma, size) | Lognormal distribution | mean: Mean of logarithm (default: 0)sigma: Standard deviation of logarithm (default: 1)size: Sample size (default: 1) | | pareto(alpha, size) | Pareto distribution | alpha: Shape parameter (default: 1)size: Sample size (default: 1) | | triangular(low, high, mode, size) | Triangular distribution | low: Lower bound (default: 0)high: Upper bound (default: 1)mode: Mode (default: 0.5)size: Sample size (default: 1) |

Discrete Distributions

| Function | Description | Parameters | |----------|-------------|------------| | randint(low, high, size) | Discrete uniform distribution | low: Lower bound (included, default: 0)high: Upper bound (excluded)size: Sample size (default: 1) | | poisson(lambda, size) | Poisson distribution | lambda: Rate parameter (default: 1)size: Sample size (default: 1) | | binomial(n, p, size) | Binomial distribution | n: Number of trialsp: Success probabilitysize: Sample size (default: 1) | | geometric(p, size) | Geometric distribution | p: Success probabilitysize: Sample size (default: 1) | | chisquare(df, size) | Chi-square distribution | df: Degrees of freedomsize: Sample size (default: 1) |

Multivariate Distributions

| Function | Description | Parameters | |----------|-------------|------------| | multivariateNormal(mean, cov, size) | Multivariate normal distribution | mean: Mean vectorcov: Covariance matrixsize: Sample size (default: 1) |

Utilities

| Function | Description | Parameters | |----------|-------------|------------| | seed(value) | Set random seed | value: Seed value | | shuffle(array) | Randomly shuffle array | array: Array to shuffle | | permutation(n) | Random permutation of integers | n: Number of elements | | randString(length) | Random string | length: Length of the string | | mixture(distributions, weights, size) | Mixture of distributions | distributions: Array of distribution functionsweights: Array of weightssize: Sample size (default: 1) |

Examples

Monte Carlo Simulation

import { uniform } from 'randlibjs';

function estimatePi(samples = 1000) {
  let insideCircle = 0;
  
  for (let i = 0; i < samples; i++) {
    const x = uniform(-1, 1);
    const y = uniform(-1, 1);
    
    if (x*x + y*y <= 1) {
      insideCircle++;
    }
  }
  
  return 4 * insideCircle / samples;
}

// Estimate π with 10,000 samples
console.log(estimatePi(10000));

Random Walk Simulation

import { randint } from 'randlibjs';

function randomWalk(steps = 100, p = 0.5) {
  let position = 0;
  const positions = [position];
  
  for (let i = 0; i < steps; i++) {
    // Movement: +1 with probability p, -1 with probability 1-p
    const step = Math.random() < p ? 1 : -1;
    position += step;
    positions.push(position);
  }
  
  return positions;
}

// Perform a random walk of 100 steps
const walk = randomWalk(100);
console.log(walk);

Documentation

For detailed documentation and more examples, visit:

License

BSD-3-Clause License

Author

Adrián Muñoz Perera