Randomized.js
Randomized.js is simple javascript library, that can makes Randomizing and Seeding easier.
Download
You can download randomized-js with npm and require it:
npm install randomized-jsconst Random =
Or you can just import it with ES6 modules:
Usage
Main methods for creating random numbers are Random.number(min,max,seed?)
and Random.random(seed?)
You can also use Random.pick(arr,seed)
and Random.randomize(arr,seed)
to pick/randomize your array (examples below);
//Random.number(min,max,seed?/floor?,floor?);Random // Alway 2Random // Always 0.6962755533213519 Random // Returns 5Random // returns [1, 3, 4, 2]
You can also use new Random(seed?)
constructor to create instance of Random.
If you dont provide seed, it will just use Math.random()
to generate numbers.
const rnd = "Hello World"rnd // Always 0.5423943877054241//Usage: rnd.next(min,max,floor?)rndnext09false // Always 7.731296254855902rndnext09true // Always 3rnd; // Reseeds to "Random.js"rndnext09 // Always 8
Everytime you run rnd.next()
, the next generated value will be different! (example below)
const rnd = "See!";rndnext010; //Always returns 9rndnext010; //Always returns 8rndnext010; //Always returns 0
Every time you run rnd.random(seed)
or rnd.next(min,max)
, next random number will be changed
If you want generate one random number with/without seed you can use static function Random.number(seed)
or Random.number(seed)
:
//Usage: Random.number(min,max,seed?/floo?r,floor?)Random // returns 1 or 2Random // returns 0 or 1Random // Always returns 2Random // returns a number from 6 to 9 with decimal numbersRandom // again returns a number from 6 to 9 with decimal numbersRandom // Always 15.03660798224236Random // Always 9.783848699829104//As you can see, even with the same seed the number is different //Usage Random.random(seed?)Random // Always 0.8273460546070028Random // Random number from 0-1
There are two more static functions! Random.pick(array)
which picks a random item from array (It also supports string) and Random.randomize(array)
which randomizes array, string or number
Both supports seeding
// Usage Random.pick(input,seed?)Random // Returns 1 , 2 or 3Random // Returns 3Random // Returns 5 Random // Returns h, e, l, o, w, r or dRandom // Returns e //Usage Random.pick(input,seed?,rolls?)Random // returns [2, 1, 4, 5, 6, 3]Random // returns [1, 3, 4, 2]Random // returns randomized array ex.