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

distributed-randomizer

v1.0.0

Published

DistRND.js is a randomizer which distributes the numbers evenly.

Downloads

2

Readme

DistRND.js

NPM version NPM Downloads

DistRND is a small PRNG (pseudorandom number generator) which also evenly distributes the random values over the given range.

■ Install

<script src="DistRND.min.js"></script>

■ Install (NPM)

npm i distributed-randomizer

■ Use

//Add this line when using NPM:
//const DistRND = require('distributed-randomizer');

const rand = new DistRND(min, max);
rand.next();

■ Parameters

argument|data type|description|default| ---|---|---|--- min|positive int|Minimum value max|positive int|Maximum value spread|positive int|High spread will distribute numbers more randomly|0

Note: next() will behave more and more like Math.random() with increasing high spread value.

Tip: Start with spread = 1 and increase slowly to see the effects.

■ Showcase

Let's assume we want to generate 10.000 random numbers between 1 and 10. We will count the occurrence of each number for demonstrating purposes:

Comparison:

let data1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let data2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (let i = 0; i < 10000; i++) {
    data1[Math.floor(Math.random() * 10)]++;    //with Math.random()
    data2[rand.next()]++;                       //with DistRND.js
}

Results:

data1 (sorted): [934, 956, 957, 997, 1007, 1016, 1018, 1034, 1039, 1042]
data2 (sorted): [1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]

As you can see the normal Math.random() function can vary quite a bit from the average due to the nature of randomness or at least pseudorandomness in JavaScript. In this case the deviation from 1000 was ~3% on average and ~6.5% maximum.

■ Speed

DistRND.js can generate 1 Mio. random numbers between 1 and 1000 in less than 100 ms.

■ Implementation

  • Generate random number x from active scope
  • Add 1 to occurrence of x
  • If new occurrence of x is greater than the average: Drop x from active scope
  • If average updates: Reevaluate active scope

■ FAQ

• Is it still random?

Yes. The numbers in the active scope are still chosen at random with Math.random().

• Why manipulate randomness?

Example 1: When you want to train a Neural Network you could use this to train it with random sample data but also ensure that it won't get overtrained.

Example 2: When you have a game with multiple players you could use this to select random players but also ensure that every player gets the same amount of turns over the entire game.