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

arbit

v0.1.1

Published

A tiny pseudo-random number generator.

Downloads

233

Readme

arbit

A tiny pseudo-random number generator.

Example

var arbit = require('arbit');

var random = arbit();

var HIT_CHANCE = 0.75;
var MIN_DAMAGE = 15, MAX_DAMAGE = 21;

if (random() <= HIT_CHANCE) {
  var damage = random.nextInt(MIN_DAMAGE, MAX_DAMAGE + 1);
  console.log('You hit the zombie for %d damage!', damage);
} else {
  console.log('You missed!');
}

Function reference

var generator = arbit(seed)

The package itself is just a single function which returns a number generator using the provided seed. The seed can be any string.

var generator = arbit.fromState(state)

Returns a number generator initialized to be in the provided state.

var number = generator()

Calling the generator itself returns a value greater than or equal to zero, and less than one.

var number = generator.nextFloat(max)

Returns a floating point that somewhere between zero and max. If a negative number is provided, the range will instead be (max, 0].

var number = generator.nextFloat(min, max)

Returns a floating point number within either the range [min, max) or (max, min] (if the range is reversed).

var number = generator.nextInt(max)

Same as nextFloat(max), but coerces the value to an integer with Math.floor.

var number = generator.nextInt(min, max)

Same as nextFloat(min, max), but coerces the value to an integer with Math.floor.

var state = generator.getState()

Returns the current state of the generator. This can be passed into arbit.fromState(state) to get back another generator in the same state (i.e., it will generate the same sequence of numbers).

Why arbit?

Reproducibility

This library provides guaranteed reproducibility of observed sequences of numbers, given that you supply the same state to the PRNG. This is not possible with Math.random.

Simplicity

arbit is very small but provides additional functions for getting random ranges and integers.

Quality

Dilbert on randomness

It's very difficult to reason about what is random and what is not, especially as a human being (we tend to see patterns where there are none). Some important qualities to look for in a PRNG are:

  • No predictable/repeating patterns
  • Even distribution of numbers (no number is more likely than another)
  • Number of possible unique patterns that can be generated

Verifying the quality

In this repo you will find the script dieharder.bash. Running it will generate a ~5 GB file sampling numbers from arbit, then pass it on to Dieharder which will test the quality of the output (how unpredictable it is).

Compared to Math.random, arbit scores better in terms of randomness quality.

Before you can run the script, you need to install Dieharder. If you have Homebrew installed, doing so is easy:

brew install dieharder

You can now run the test:

./dieharder.bash

Have a look at an example output. You can compare this to Math.random's output.

Performance

The goal of this library is to provide simple, good, reproducible, and performant pseudo-random number generation. Here is a benchmark between Math.random and arbit, running on Node.js (v0.12.5):

arbit x 55,539,526 ops/sec ±3.12% (93 runs sampled)
Math.random x 62,497,530 ops/sec ±1.02% (93 runs sampled)