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

prng-well1024a

v2.0.0

Published

Random number generator based on WELL-1024a algorithm.

Downloads

34,949

Readme

well1024a.js

NPM

This is a Javascript implementation of the WELL-1024a pseudorandom number generation algorithm.

Javascript's built-in Math.random() function is implementation-dependent and therefore of limited usefulness if your program depends on random numbers, as you risk running into crappy implementations. Even the V8 engine (used by Node.js) only provides 32-bit entropy, and is based on the platform-dependent C++ rand() function.

This module is very bare-bones. I have also written a randomness library called randy that provides useful functions like RandInt(min, max), shuffle(array) etc., based on this module.

Quick Example

var rng = well1024a();
var number = rng.getUInt32();
var coin = ['heads', 'tails'][number % 2];
// coin == 'heads'

For Node.js, use npm:

npm install prng-well1024a

Download and include as a <script>. The module will be available as the global object randy.

Development: well1024a.js - 2Kb Uncompressed

Production: well1024a.min.js - < 1Kb Minified

Example

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

I am <span id="age"></span> years old!

<script>
    var n = document.getElementById("age");
    var myAge = well1024a.getUInt32();
    n.innerText = myAge.toString();
</script>

Documentation

Constructor

Instance Functions


Returns a new well1024a instance, which is an object with 3 functions:

The instance will use Math.random() to fill out the initial seed state.

Arguments

  • entropy - default=[]. Array of numbers to add to the initial seed. These should be based on environmental values that are likely to be different on each run such as system time, process ID, browser window height, values from /dev/urandom etc.

Example

var w = well1024a([
    Date.now(),
    os.freemem(),
    process.pid
]);

Returns a random positive integer less than 2^32.

Example

var w = well1024a();

console.log('For Christmas this year, I want ' + w.getUInt32().toString() + ' ponies!');

Returns an array of 32-bit unsigned integers, of length 32. This represents the current state of the random number generator.

This array can be used as a parameter to setState.


Sets the random number generator to a specific state, allowing for replay of random values.

General use case is to give it a value previously received by calling getState().

Arguments

  • state - Must be an array of 32-bit unsigned integers, of length 32.

Example

This will flip a pair of coins, reset the generator state, and flip the coins again with the exact same output.

var w = well1024a();
var coins = ['heads', 'tails'];

console.log("Flippin' the coins:");
var state = w.getState();
var d1 = coins[w.getUInt32() % 2];
var d2 = coins[w.getUInt32() % 2];
console.log(d1 + " and " + d2);

console.log("Instant replay:");
w.setState(state);
d1 = coins[w.getUInt32() % 2];
d2 = coins[w.getUInt32() % 2];
console.log(d1 + " and " + d2);

Notes

No functions rely on this, so it's safe to e.g. assign randy.good.randInt to a variable or pass it around as a parameter.