Aimless
is the missing js random library. It’s small (<6 KB), anonymous/unopinionated, and dependency-free, and provides a variety of useful random number utilities. Best of all, it’s compatible with all of your favorite PRNGs.
It provides various utilities for generating random numbers, selecting random elements from arrays, getting random integers within a range, and more. You can also combine Aimless.js with your favorite PRNGs (pseudo-random number generators) to provide an extra layer of randomness to your applications.
Why @Aimless.js?
Outside of the big game engines and frameworks, there is very little support for random number generation in JavaScript. Sure, there are a plethora of alternatives to the original Math.random
function, but what if I wanted to generate a random number within the range? Better yet, what if I wanted to do anything but get a number between 0 and 1?
Browser support
Aimless is based on ES5 matrix methods, and is supported in all modern browsers. Support for older or outdated browsers is not planned.
Must Read: Easiest and Most Powerful Secure Random Number Generator | Rando.js
How to make use of it:
1. Install it and add it to your package.json dependencies.
$ npm install aimless.js --save
2. Then import the utility functions into the file where you will use them.
import { bool, intRange } from 'aimless.js'
3. Aimless.js
is compatible with any custom PRNG that returns num >= 0
and num < 1
. Each function accepts an engine to be used.
import { bool } from 'aimless.js' const engine = () => 0 bool(engine) // false
4. In addition, each function in Aimless has a counterpart called withEngine. This function will re-seal closes around your engine, so you don’t need to swipe it every time.
import { boolWithEngine } from 'aimless.js' const engine = () => 0 const bool = boolWithEngine(engine) bool() // false
Each function will be set by default to use the provided default engine if no custom engine is provided. The default engine uses crypto.getrandomvalues
when available, referencing Math.random
.
API
- bool
- char
- customDist
- exponentialDist
- floatRange
- intRange
- intSequence
- normalDist
- normalFloat
- oneOf
- seedFunc
- sequence
- sign
- uniqFuncIntRange
- uniqFuncSequence
- uuid
- weighted
Must Read: JavaScript Library to Generate Random PIN Codes
bool(engine)
Returns either true
or false
.
char(string, engine)
Returns a random character from the provided string
.
customDist(function, engine)
Returns a random number following a custom distribution of your choosing. function
should take in a number between 0
and 1
.
exponentialDist(lambda, engine)
Returns a random number following an exponential distribution with the provided lambda
.
floatRange(min, max, engine)
Returns a random float between min
and max
.
intRange(min, max, engine)
Returns a random integer between min
and max
.
intSequence(min, max, engine)
Returns an array with all integers between min
and max
in random order.
normalDist(mean, stdDev, engine)
Returns a random number following a normal distribution with mean mean
and standard deviation stdDev
.
normalFloat(engine)
Returns a random float between -1
and 1
.
oneOf(array, engine)
Returns a random item from the array
provided.
seedFunc(seed)
Returns a seeded random number generator. Seeded RNGs produce random numbers, but are predictable if you use the same seed. note: the Park-Miller PRNG is used to provide the seeded function, therefore, an engine
is not accepted.
sequence(array, engine)
Returns a new array with the same items contained in array
but in random order.
sign(engine)
Returns either -1
or 1
.
uniqFuncIntRange(min, max, engine)
Returns a unique random number between min
and max
, using the provided engine
. If no engine
is passed, the defaultEngine will be used. If there are no unique values left to return, null
will be returned.
uniqFuncSequence(array, engine)
Returns a unique random number from the provided array
, using the provided engine
. If no engine
is passed, the defaultEngine will be used. If there are no unique values left to return, null
will be returned.
uuid(engine)
Returns a valid RFC4122 version4 ID hex string, using the provided engine
.
weighted(numbers, weights, engine)
Returns one of the numbers
provided, biased towards the corresponding weights
provided. numbers
can include floats.
See Demo And Download
Official Website(ChrisCavs): Click Here
This superior jQuery/javascript plugin is developed by ChrisCavs. For extra advanced usage, please go to the official website.