glossywheel v1.9.0
GlossyWheel
A collection of utility functions for the random number generators.
setup
npm
npm i glossywheel
es6
import GlossyWheel from 'glossywheel';
node
let GlossyWheel = require('glossywheel');
browser
<script src="https://unpkg.com/glossywheel"></script>
The module is globally available as GlossyWheel
.
members
static properties
.random = Math.random
.MersenneTwister(seed)
The implementation of the Mersenne Twister pseudorandom number generator.
let random = GlossyWheel.MersenneTwister(835);
console.log(random()); // => 0.7212939869047637
console.log(random()); // => 0.2532498844651273
.lowerCasedAlphabetic = 'abcdefghijklmnopqrstuvwxyz'
.upperCasedAlphabetic = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.alphabetic = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
.numeric = '0123456789'
.lowerCasedAlphanumeric = 'abcdefghijklmnopqrstuvwxyz0123456789'
.upperCasedAlphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
.alphanumeric = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
constructor
new GlossyWheel(random)
let seed = Date.now();
let wheel1 = new GlossyWheel(GlossyWheel.MersenneTwister(seed));
let wheel2 = new GlossyWheel(GlossyWheel.MersenneTwister(seed));
let integer1 = wheel1.integer(-2, 2, true);
let integer2 = wheel2.integer(-2, 2, true);
console.log(integer1 === integer2); // => true;
instance properties
.random
An user-defined function that returns a float greater than or equal to 0 and less than 1.
static and instance methods
.boolean()
Generates a boolean.
let randomBoolean = GlossyWheel.boolean();
// => either true or false
.booleanWeighted(weight)
Generates a boolean with weighted probability.
let randomBoolean = GlossyWheel.booleanWeighted(3);
// => either true (75%) or false (25%)
console.log(GlossyWheel.booleanWeighted(0) === false); // => true
console.log(GlossyWheel.booleanWeighted(Infinity) === true); // => true
.float(min, max, inclusive = false)
Generates a float within the specified range. The minimum is always inclusive and the maximum only if specified.
let randomFloat = GlossyWheel.float(0.4, 2.7);
// => a float greater than or equal to 0.4 and less than 2.7
let randomFloat = GlossyWheel.float(0.4, 2.7, true);
// => a float greater than or equal to 0.4 and less than or equal to 2.7
let float = 6.5;
console.log(GlossyWheel.float(float, float, true) === float); // => true
.integer(min, max, inclusive = false)
Generates an integer within the specified range. The minimum is always inclusive and the maximum only if specified.
let randomInteger = GlossyWheel.integer(2, 11);
// => an integer greater than or equal to 2 and less than 11
let randomInteger = GlossyWheel.integer(2, 11, true);
// => an integer greater than or equal to 2 and less than or equal to 11
console.log(GlossyWheel.integer(4.3, 6.5) === 5); // => true
.string(alphabet, length)
Generates a string of the specified length using an array-like or iterable object as the alphabet.
let randomString = GlossyWheel.string(GlossyWheel.alphabetic, GlossyWheel.integer(8, 16, true));
// => 'eFEgkjWzTsTmA'
.date(min, max, inclusive = false)
Generates a date within the specified range. The minimum is always inclusive and the maximum only if specified.
let randomDate = GlossyWheel.date(new Date('01 Jan 1970'), new Date('04 Dec 1995'));
// => '1989-08-26T01:02:17.622Z'
let date = new Date('04 Dec 1995');
console.log(GlossyWheel.date(date, date, true).getTime() === date.getTime()); // => true
.index(array)
Selects an index from an array-like object.
let randomIndex = GlossyWheel.index(['a', 'b', 'c']);
// => either 0, 1 or 2
.indexWeighted(weightedArray)
Selects an index from an array-like object whose elements are item-weight pairs.
let randomIndex = GlossyWheel.indexWeighted([['a', 3.9], ['b', 1], ['c', 2.2]]);
// => either 0 (55%), 1 (14%) or 2 (31%)
.item(collection)
Selects an item from an array-like or iterable object.
let randomItem = GlossyWheel.item(['a', 'b', 'c']);
// => either 'a', 'b' or 'c'
.itemWeighted(weightedCollection)
Selects an item from an array-like or iterable object whose elements are item-weight pairs.
let randomItem = GlossyWheel.itemWeighted([['a', 3.9], ['b', 1], ['c', 2.2]]);
// => either 'a' (55%), 'b' (14%) or 'c' (31%)
.items(collection, maxCount)
Selects items without repetition from an array-like or iterable object. Preserves the order of the items.
let randomItems = GlossyWheel.items(['a', 'b', 'c', 'd', 'e'], 3);
// => ['a', 'b', 'd']
.itemsWeighted(weightedCollection, maxCount)
Selects items without repetition from an array-like or iterable object whose elements are item-weight pairs. Preserves the order of the items.
let randomItems = GlossyWheel.itemsWeighted([['a', 2], ['b', 7], ['c', 8], ['d', 5], ['e', 8]], 3);
// => ['a', 'c', 'e']
.itemsRepeated(collection, count)
Selects items with repetition from an array-like or iterable object.
let randomItems = GlossyWheel.itemsRepeated(['a', 'b'], 8);
// => ['b', 'b', 'a', 'a', 'b', 'b', 'a', 'b']
.itemsRepeatedWeighted(weightedCollection, count)
Selects items with repetition from an array-like or iterable object whose elements are item-weight pairs.
let randomItems = GlossyWheel.itemsRepeatedWeighted([['a', 1], ['b', 3]], 8);
// => ['b', 'b', 'b', 'b', 'a', 'b', 'b', 'b']
.shuffle(collection)
Shuffles an array-like or iterable object.
let shuffledItems = GlossyWheel.shuffle([1, 2, 3, 4, 5]);
// => [4, 3, 2, 5, 1]
.shuffleInPlace(array)
Shuffles an array-like object in place.
let items = [1, 2, 3, 4, 5];
let shuffledItems = GlossyWheel.shuffleInPlace(items);
console.log(shuffledItems === items); // => true
plugins
Write your own plugins to extend the library.
import GlossyWheel from 'glossywheel';
GlossyWheel.extend({
color() {
return `rgb(${[0, 0, 0].map(() => this.integer(0, 255, true)).join(',')})`;
},
});