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

arbitrary

v1.4.10

Published

Efficient Reversible Number Generator

Downloads

102

Readme

Overview

A deterministic and reversible random number generator.

Ideal for generative art, as well as games for varied entity behavior.

Features

  • Is deterministic (provide the same seed to get same stream of random values)
  • Is reversible (see next/prev section)
  • Internal state only takes up 32bits
  • Has a period length of 2^32
  • Each value from [0, 2^32) is generated exactly once in the total period
  • Has great test coverage

Demos

Installation

npm install arbitrary

Usage

Basics

import arbitrary from 'arbitrary';

// Creates a Generator with a random seed.
let generate = new arbitrary.Generator();

console.log( generate.next.number(-10, 10) );
console.log( generate.next.percent() );
console.log( generate.next.bits(1) );
console.log( generate.next.bits(32) );

Deterministic

import arbitrary from 'arbitrary';
// Create a Generator with a seed
let deterministic = new arbitrary.Generator(42);

// Will always be: 0.2523451747838408
console.log( deterministic.next.percent() );
// Will always be: 22
console.log( deterministic.next.bits(8) );
// Will always be: 5772.811982315034
console.log( deterministic.next.number(0, 10000) );

Reversing

import arbitrary from 'arbitrary';

// Create a Generator with a seed
let generate = new arbitrary.Generator();

console.log( generate.next.number(0, 10000) );
console.log( generate.next.number(-10, 10) );
console.log( generate.next.percent() );
console.log( generate.next.bits(1) );
console.log( generate.next.bits(32) );

// Now generate the reverse stream of the above numbers
console.log( generate.prev.bits(1) );
console.log( generate.prev.percent() );
console.log( generate.prev.number(-10, 10) );
console.log( generate.prev.number(0, 10000) );

/**
Output will be:

    0.734807450324297
    -3.3194907444994897
    0.9332054262049496
    1
    3735025540
    1
    0.9332054262049496
    -3.3194907444994897
    0.734807450324297
*/

Scramble / Descramble

The scramble function is best used for turning sequences of ordered numbers (Ex. an increasing counter) and scrambling the bits to get random number. It also reversible via arbitrary.descramble().

A few obvious utilizing scrambling:

  1. Jump back and forward in a stream of randomly generated numbers.
  • To do this, keep an index into a sequence and scramble the index you want a random number. Just jump or set the index to another point and scramble it again to recoup the random number generated at that point in time.
  • For comparison using Generator .next/.prev can be forwarded/reversed, but it can only do so a single step at a time, making large jumps in a sequence take proportionally more CPU per distance jumped.
  1. Use scramble to take a Generate state and jump to another point in the sequence it generates.
  • This is useful for instance when creating generative art and wanting to fork a new stream of random values.

Important: Use Generator .next/.prev if you don't need this focus but just want a series of random numbers as scramble/unscramble are computationally more expensive.

import arbitrary from 'arbitrary';

// Scramble the bits of an unsigned 32 bit integer
const scrambled42 = arbitrary.scramble(42);
    // Should print '1077848774'
console.log(scrambled42);

// Reverse the scrambling to get back the original number
const unscrambled42 = arbitrary.descramble(scrambled42);
    // Should print '42'
console.log(unscrambled42)

API Reference

Coming soon. See examples above.

Contributing

Setup

git clone [email protected]:francoislaberge/arbitrary.git
cd arbitrary

Watching

npm run watch

Building

npm run build

Publishing to NPM

Do the usual npm version bump then publish.

npm version <major|minor|patch>
git push; git push --tags
npm publish

Test

The tests must be built before they can be run. So use either the build or watch approach below before running the tests.

All tests are run automatically on push via our travis-ci integration

npm run test

Watch Tests

Automatically builds tests when any source code changes (test or regular).

npm run watch-test

Build Tests

npm run build-test