@daeinc/random
v0.4.1
Published
Random utilities
Downloads
19
Readme
@daeinc/random
Random utilities. Mainly a collection of convenience functions. The functions are designed to be used with existing random libraries. It has no dependency on any particular random libraries but it's best to use with those seeded ones.
The function signatures are important. The chosen random functions have to support randFn(min, max)
, randFn(max)
and shuffleFn(arr)
, etc. My current choice is canvas-sketch-util
.
Installation
npm i @daeinc/random
then,
import { sample, ... } from "@daeinc/random"
Types
declare type RandFn = (n: number) => number;
declare type ShuffleFn = <T>(arr: T[]) => T[];
Functions
boolean
function boolean<T>(prob: number, randFn: RandFn): boolean;
function boolean<T>(prob: number, randFn: RandFn, optTrue: NonNullable<T>, optFalse: NonNullable<T>): T;
Returns either true
or false
given a probability, prob
between 0 and 1. It can also return custom value.
function booleanFnCreator(randFn: RandFn): (prob: number) => boolean;
function booleanFnCreator<T>(randFn: RandFn, optTrue: NonNullable<T>, optFalse: NonNullable<T>): (prob: number) => T;
Convenience function so as not to input the same seeded function all the time when using boolean()
. It returns a seeded boolean function.
sample
const sample: <T>(arr: NonNullable<T>[], randFn: RandFn) => T;
Samples a random element from array with seeded random function
sampleGaussian
const sampleGaussian: <T>(arr: NonNullable<T>[], mean: number | undefined, stddev: number | undefined, gaussianFn: (mean: number, stddev: number) => number) => T;
(This function is not quite ready)
sampleMultiple
const sampleMultiple: <T>(arr: NonNullable<T>[], numSamples: number, shuffleFn: ShuffleFn) => T[];
Samples multiple elements from the original array. Returns a new array.
sampleWeighted
const sampleWeighted: <T>(values: T[], weights: number[], randFn: RandFn) => T;
Samples a random value based on weights
array. The value returned can be any type, even an array.
shuffle
const shuffle: <T>(arr: T[], shuffleFn: ShuffleFn) => T[];
Shuffles array. Returns a new array.
To Dos
- try
random-js
- REVIEW: https://attacomsian.com/blog/javascript-deep-clone-array
License
MIT