JustMyLuck
A collection of utility functions for working with randomness.
setup
npm
npm i just-my-luck
ES module
;
Node
let JustMyLuck = ;
browser
The module is globally available as JustMyLuck
.
members
static properties
.random = Math.random
.MersenneTwister(seed)
The implementation of the Mersenne Twister pseudo-random number generator.
argument | description |
---|---|
seed |
A number as the initial seed. |
Returns the pseudo-random number generator as a function.
let random = JustMyLuck;console; // => 0.7212939869047637console; // => 0.2532498844651273
.lowerCasedAlphabetic = 'abcdefghijklmnopqrstuvwxyz'
.upperCasedAlphabetic = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.alphabetic = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
.numeric = '0123456789'
.lowerCasedAlphanumeric = 'abcdefghijklmnopqrstuvwxyz0123456789'
.upperCasedAlphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
.alphanumeric = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
constructor
new JustMyLuck(random)
argument | description |
---|---|
random |
A function that returns a float greater than or equal to 0 and less than 1. |
let seed = Date;let luck1 = JustMyLuck;let luck2 = JustMyLuck;let integer1 = luck1;let integer2 = luck2;console; // => true;
instance properties
.random
The pseudo-random number generator.
static and instance methods
.chance(p)
Tests the chance with a probability.
argument | description |
---|---|
p |
A number as the probability. |
Returns a boolean.
let x = JustMyLuck;// => either true (38%) or false (62%)
console; // => falseconsole; // => falseconsole; // => false console; // => trueconsole; // => trueconsole; // => true
.boolean()
Generates a boolean.
Returns the generated boolean.
let boolean = JustMyLuck;// => either true or false
.booleanWeighted(weight)
Generates a boolean using a weighted probability.
argument | description |
---|---|
weight |
A number as the weighted probability. |
Returns the generated boolean.
let boolean = JustMyLuck;// => either true (75%) or false (25%)
console; // => falseconsole; // => false console; // => true
.float(min, max, inclusive = false)
Generates a float within a range.
argument | description |
---|---|
min |
A number as the inclusive minimum. |
max |
A number as the exclusive maximum. |
inclusive |
If true , the maximum is inclusive. |
Returns the generated float.
let float = JustMyLuck;// => a float greater than or equal to 0.4 and less than 2.7
let float = JustMyLuck;// => a float greater than or equal to 0.4 and less than or equal to 2.7
let float = 65;console; // => true
.integer(min, max, inclusive = false)
Generates an integer within a range.
argument | description |
---|---|
min |
A number as the inclusive minimum. |
max |
A number as the exclusive maximum. |
inclusive |
If true , the maximum is inclusive. |
Returns the generated integer.
let integer = JustMyLuck;// => an integer greater than or equal to 2 and less than 11
let integer = JustMyLuck;// => an integer greater than or equal to 2 and less than or equal to 11
console; // => true
.string(alphabet, length)
Generates a string using an alphabet.
argument | description |
---|---|
alphabet |
An array-like or iterable object of characters as the alphabet. |
length |
A number as the length of the string to generate. |
Returns the generated string.
let string = JustMyLuck;// => e.g. 'eFEgkjWzTsTmA'
.date(min, max, inclusive = false)
Generates a Date
instance within a range.
argument | description |
---|---|
min |
A Date instance as the inclusive minimum. |
max |
A Date instance as the exclusive maximum. |
inclusive |
If true , the maximum is inclusive. |
Returns the generated Date
instance.
let date = JustMyLuck;// => e.g. '1989-08-26T01:02:17.622Z'
let date = '04 Dec 1995';console; // => true
.pick(collection)
Picks a value from a collection.
argument | description |
---|---|
collection |
An array-like or iterable object to pick from. |
Returns the picked value.
let value = JustMyLuck;// => either 'a', 'b' or 'c'
.pickWeighted(weightedCollection)
Picks a value from a collection using a weighted probability.
argument | description |
---|---|
weightedCollection |
An array-like or iterable object of value-weight pairs. A value-weight pair is an array where the first item is the value and the second item is a number as the weighted probability for this value to be picked. |
Returns the picked value.
let value = JustMyLuck;// => either 'a' (66%), 'b' (14%) or 'c' (20%)
let value = JustMyLuck;// => always 'a'
let value = JustMyLuck;// => either 'b' or 'c'
.pickIndex(array)
Picks an index from an array.
argument | description |
---|---|
array |
An array-like object to pick the index from. |
Returns the picked index.
let index = JustMyLuck;// => either 0, 1 or 2
.pickCombination(collection, count)
Picks a combination of values without repetition from a collection. Preserves the order of the values.
argument | description |
---|---|
collection |
An array-like or iterable object to pick from. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['a', 'b', 'd']
.pickCombinationWeighted(weightedCollection, count)
Picks a combination of values without repetition from a collection using a weighted probability. Preserves the order of the values.
argument | description |
---|---|
weightedCollection |
An array-like or iterable object of value-weight pairs. A value-weight pair is an array where the first item is the value and the second item is a number as the weighted probability for this value to be picked. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['a', 'c', 'e']
let values = JustMyLuck;// => 'a' and 'e' are always included, 'c' is never included
.pickPermutation(collection, count)
Picks a permutation of values without repetition from a collection.
argument | description |
---|---|
collection |
An array-like or iterable object to pick from. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['b', 'd', 'a']
.pickPermutationWeighted(weightedCollection, count)
Picks a permutation of values without repetition from a collection using a weighted probability.
argument | description |
---|---|
weightedCollection |
An array-like or iterable object of value-weight pairs. A value-weight pair is an array where the first item is the value and the second item is a number as the weighted probability for this value to be picked. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['c', 'e', 'a']
let values = JustMyLuck;// => 'a' and 'e' are always included, 'c' is never included
.pickMulticombination(collection, count)
Picks a combination of values with repetition from a collection. Preserves the order of the values.
argument | description |
---|---|
collection |
An array-like or iterable object to pick from. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b']
.pickMulticombinationWeighted(weightedCollection, count)
Picks a combination of values with repetition from a collection using a weighted probability. Preserves the order of the values.
argument | description |
---|---|
weightedCollection |
An array-like or iterable object of value-weight pairs. A value-weight pair is an array where the first item is the value and the second item is a number as the weighted probability for this value to be picked. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['a', 'b', 'b', 'b', 'b', 'b', 'b', 'b']
let values = JustMyLuck;// => ['a', 'a', 'a', 'a']
let values = JustMyLuck;// => ['b', 'b', 'b', 'b']
.pickMultipermutation(collection, count)
Picks a permutation of values with repetition from a collection.
argument | description |
---|---|
collection |
An array-like or iterable object to pick from. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['a', 'a', 'b', 'b', 'a', 'a', 'b', 'a']
.pickMultipermutationWeighted(weightedCollection, count)
Picks a permutation of values with repetition from a collection using a weighted probability.
argument | description |
---|---|
weightedCollection |
An array-like or iterable object of value-weight pairs. A value-weight pair is an array where the first item is the value and the second item is a number as the weighted probability for this value to be picked. |
count |
A number as the count of the values to pick. |
Returns the picked values as an array.
let values = JustMyLuck;// => e.g. ['b', 'b', 'b', 'a', 'b', 'b', 'b', 'b']
let values = JustMyLuck;// => ['a', 'a', 'a', 'a']
let values = JustMyLuck;// => ['b', 'b', 'b', 'b']
.shuffle(collection)
Shuffles a collection.
argument | description |
---|---|
collection |
An array-like or iterable object to shuffle. |
Returns the shuffled collection as an array.
let values = JustMyLuck;// => e.g. [4, 3, 2, 5, 1]
.shuffleInPlace(array)
Shuffles an array in place.
argument | description |
---|---|
array |
An array-like object to shuffle. |
Returns the same array but shuffled.
plugins
Write your own plugins to extend the library.
; JustMyLuck;