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

ndrand

v1.0.0

Published

A module to generate a nonuniform distribution of random numbers

Downloads

2

Readme

ndrand

A class used to generate a nonuniform distribution of random numbers.

Usage

var ndr = require('ndrand');

ndr.initialize([
  {x:0,y:0},
  {x:5,y:5},
  {x:10,y:0},
]);

console.log(ndr.random());

Algorithm Initialization

The user initializes the class with an array of x,y pairs describing the desired distribution. Two consecutive pairs math and math form a trapezoid like so.

trapezoid

Where the height of two sides are math and math respectively and the width is math

The area of the trapeziod is the average of the two heights multiplied by the width and can be written like so.

math

During initialization, we calculate the area of each trapezoid to obtain the total area of the distribution. Along the way we record the cumulative area (cumArea) at each pair. This represents the area of all trapezoids to the left of the current pair. As such, the cumulative area of the first pair is 0 and the cumulative area of the last pair is the total area of the distribution.

We then go back and using the cumulative area of each pair and total area previously computed, calculate the cumulative distribution (cumDist) of each pair. The distribution represents the percentage of numbers in the distribution to the left of the current pair. Consequently, the cumulative distribution of the first pair is 0 and the cumulative distribution of the last pair is 1.0

Generation Algorithm

Given a uniform distributed random (udr) number from 0-1 we treat this as a percentage of the overall area of the nonuniform distribution curve (ndc) and try to find the value math where the ratio of the area of the ndc to left to the total area exactly matches the udr.

We do this by first determining which trapezoid that contains the area we are looking for. This will be the trapezoid formed by the consecutive pairs math and math where the cumDist of math is less than udr and the cumDist of math is greater than udr. From there we calculate the target volume by multiplying the udr by the cumArea of the last pair in the ndc and then subtract the cumArea of the math pair to obtain the math. This is the area of the trapezoid formed by the pairs math and math in the figure below.

partial trapezoid

Given math we can calculate math using the the previous equation for the area of a trapezoid like so.

math where math

substituting math we can simplify this as

math

math

math

math

which is a quadratic equation of the form

math

where

math

math

math

thus we can solve for math using the quadratic equation

math

The term math is called the axis of symetry (aos) whereas math represents the distance (distance) from the aos

The algorithm will add or subtract the distance form the aos to find the value that falls between math and math