0.0.2 • Published 6 months ago

rng-utils v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

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

ParamDescription
minThe minimum value
maxThe 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

ParamDescription
minThe minimum value
maxThe maximum value

randomOption

Returns a random option from a provided list of options.

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

Kind: global constant

ParamDescription
optionsAn array of options to choose from

randomBool

Returns a random boolean given a weight (optional).

randomBool(.2); // false

Kind: global constant

ParamDefaultDescription
weight.5A 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

ParamDescription
optionsoptions 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

ParamDefaultDescription
mean0The number around which the distribution is centered
std.5The amount that the random number will deviate around the center
0.0.2

6 months ago

0.0.1

6 months ago

1.0.0

10 months ago