Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

arbit

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

arbit

A tiny pseudo-random number generator.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
40
decreased by-28.57%
Maintainers
1
Weekly downloads
 
Created
Source

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)

Keywords

FAQs

Package last updated on 29 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc