Skip to content

liamegan/fxhash-helpers

Repository files navigation

FX-Hash Helpers

Installation

Install the package using NPM:

npm install @liamegan1/fxhash-helpers

Quick-start

To get started with the library:

  • Import what you need as well as the FXInit function;
  • Initialise the library by passing the fxhash function to FXInit;
  • Use your helpers!
// import
import { FXInit, FXRandomBetween } from "@liamegan1/fxhash-helpers"

// Make sure you call FXInit before using any of the helpers!
FXInit( fxrand );

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

Click here for a codesandbox demonstrating how this all works.

Details## Constants

FXInit

Initialises the library with a PRNG function. Most functions expect the PRNG to return 0-1 exclusive.

FXRandomBetween

Returns a random float between two numbers.

FXRandomBetween(-10, 10); // -1.234576
FXRandomIntBetween

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

FXRandomIntBetween(-10, 10); // 2
FXRandomOption

Returns a random option from a provided list of options.

FXRandomOption(["I", "are", "weasel"]); // "weasel"
FXRandomBool

Returns a random boolean given a weight (optional).

FXRandomBool(.2); // false
FXRandVec2

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

FXRandVec2(); // [.1234, .57351]
FXRandVec3

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

FXRandVec3(); // [.1234, .57351, .01234]
FXRandVec4

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

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

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

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

Curtesy Mark Knol, T: @mknol

FXRandomGaussian

Returns a gaussian distributed random number. Bear in mind that calling FXRandomGaussian(5) will result in 5 calls to fxrand();

let gr = FXRandomGaussian(5);
FXRandomReset

Resets the fxhash prng either to a new, random hash, or to a supplied hash. Use this if you want to reset the prng to its original state by calling:

FXRandomReset(fxhash);

or to a new, random hash simply by calling

FXRandomReset();

Note that resetting to a random hash uses the existing fxhash prng, which means that random hashes are also deterministic.

Functions

check()

Checks for the existance of the FXHash PRNG and throws an error if it doesn't exist.

FXInit

Initialises the library with a PRNG function. Most functions expect the PRNG to return 0-1 exclusive.

Kind: global constant

Param Description
prng The PRNG function to use for subsequent calls

FXRandomBetween

Returns a random float between two numbers.

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

Kind: global constant

Param Description
min The minimum value
max The maximum value

FXRandomIntBetween

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

FXRandomIntBetween(-10, 10); // 2

Kind: global constant

Param Description
min The minimum value
max The maximum value

FXRandomOption

Returns a random option from a provided list of options.

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

Kind: global constant

Param Description
options An array of options to choose from

FXRandomBool

Returns a random boolean given a weight (optional).

FXRandomBool(.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

FXRandVec2

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

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

Kind: global constant

FXRandVec3

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

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

Kind: global constant

FXRandVec4

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

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

Kind: global constant

FXGetWeightedOption

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

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

Curtesy Mark Knol, T: @mknol

Kind: global constant

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

FXRandomGaussian

Returns a gaussian distributed random number. Bear in mind that calling FXRandomGaussian(5) will result in 5 calls to fxrand();

let gr = FXRandomGaussian(5);

Kind: global constant

Param Description
samples The number of samples to use in the distribution. A higher sample number will result in a tighter bell-curve

FXRandomReset

Resets the fxhash prng either to a new, random hash, or to a supplied hash. Use this if you want to reset the prng to its original state by calling:

FXRandomReset(fxhash);

or to a new, random hash simply by calling

FXRandomReset();

Note that resetting to a random hash uses the existing fxhash prng, which means that random hashes are also deterministic.

Kind: global constant

Param Type Description
newhash string [undefined] - A string value of the new hash to use. Mostly this parameter is used to reset the hash to the original.

check()

Checks for the existance of the FXHash PRNG and throws an error if it doesn't exist.

Kind: global function

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published