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

rng-utils

v0.0.2

Published

Some simple to use RNG utilities

Downloads

2

Readme

Random number utils

Installation

Install the package using NPM:

npm install rng-utils

Quick-start

To get started with the library:

  • Import what you need!
  • If you want to supply a different RNG, do so using setRNG.
  • Use your helpers!
// import
import { setRNG, randomBetween, gaussianRandom } from "@liamegan1/fxhash-helpers"

// If you want to set the functions to use a different RNG, say something seeded, do so using setRNG. By default, the library just uses Math.random (PRNG below curtesy of fxhash, MIT - https://github.com/fxhash/fxhash-boilerplate)
// There are hundreds of different javascript PRNGs out there. Find one you like :)
var alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
function b58dec(str) {
  return [...str].reduce(function(p, c) {
    return p * alphabet.length + alphabet.indexOf(c) | 0;
  }, 0);
}
function sfc32(seed) {
  let a = seed[0] | 0;
  let b = seed[1] | 0;
  let c = seed[2] | 0;
  let d = seed[3] | 0;
  return function() {
    a |= 0;
    b |= 0;
    c |= 0;
    d |= 0;
    const t = (a + b | 0) + d | 0;
    d = d + 1 | 0;
    a = b ^ b >>> 9;
    b = c + (c << 3) | 0;
    c = c << 21 | c >>> 11;
    c = c + t | 0;
    return (t >>> 0) / 4294967296;
  };
}
function matcher(str, start) {
  return str.slice(start).match(new RegExp(".{" + (str.length - start >> 2) + "}", "g")).map(function(substring) {
    return b58dec(substring);
  });
}
const R = sfc32(matcher('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ', 0));

setRNG( R );

// Good to go!
const randomAngle = randomBetween(-Math.PI, Math.PI);

Details## Constants

randomBetween

Returns a random float between two numbers.

randomBetween(-10, 10); // -1.234576

Kind: global constant

| Param | Description | | --- | --- | | min | The minimum value | | max | The maximum value |

randomIntBetween

Returns a random integer between two numbers - min, and max exclusive of max. If you want it to be inclusive of max, set the upper number to a floating point number like 10.99

randomIntBetween(-10, 10); // 2

Kind: global constant

| Param | Description | | --- | --- | | min | The minimum value | | max | The maximum value |

randomOption

Returns a random option from a provided list of options.

randomOption(["I", "are", "weasel"]); // "weasel"

Kind: global constant

| Param | Description | | --- | --- | | options | An array of options to choose from |

randomBool

Returns a random boolean given a weight (optional).

randomBool(.2); // false

Kind: global constant

| Param | Default | Description | | --- | --- | --- | | weight | .5 | A weight to test the boolean against, if fxrand is less than this number, true is returned. Defaults to 0.5 |

randVec2

Returns a 2-dimensional vector, expressed as an array, populated with random numbers

randVec2(); // [.1234, .57351]

Kind: global constant

randVec3

Returns a 3-dimensional vector, expressed as an array, populated with random numbers

randVec3(); // [.1234, .57351, .01234]

Kind: global constant

randVec4

Returns a 4-dimensional vector, expressed as an array, populated with random numbers

randVec4(); // [.1234, .57351, .01234, .9634]

Kind: global constant

weightedOption

Returns a weighted random option, given an array of options with weights.

let color = getWeightedOption([
  ["red", 10],
  ["green", 30],
  ["blue", 50],
]);

Kind: global constant

| Param | Description | | --- | --- | | options | options in the format of [ [ string: optionName, int: optionNumber ] ] |

gaussianRandom

Returns a gaussian distributed random number, centered around a mean.

let gr = gaussianRandom(5, 1);

Kind: global constant

| Param | Default | Description | | --- | --- | --- | | mean | 0 | The number around which the distribution is centered | | std | .5 | The amount that the random number will deviate around the center |