Skip to content

Instantly share code, notes, and snippets.

@BenMakesGames
Last active January 27, 2018 17:54
Show Gist options
  • Save BenMakesGames/58c5307108b74b4995c60a7b056ab04f to your computer and use it in GitHub Desktop.
Save BenMakesGames/58c5307108b74b4995c60a7b056ab04f to your computer and use it in GitHub Desktop.
Random Helpers: some JS extensions to help you generate quick random content for funsies (not for use where security is a priority)
/**
* @see {@link https://gist.github.com/BenMakesGames/58c5307108b74b4995c60a7b056ab04f}
* @file These methods make no attempt to be SECURE, in terms of the sorts of concerns that cryptography might be
* worried about. They're intended to be used to quickly make FUN things, like procedurally-generated content for a
* game. If you care about generating passwords or other secure keys, do not use these methods.
* @author Ben Hendel-Doying
*/
/**
* Returns a random integer between min and max, inclusive. (The way a human might "naturally" expect.)
*
* @example let dieRoll = Math.randomNatural(1, 6); // gets the roll of a six-sided die
*
* @returns {int}
*/
Math.randomNatural = function(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
};
/**
* Returns a random integer from 0 to max (excluding max). Useful for generating array indices, and other
* more-computer-y things.
*
* @example let randomIndex = Math.randomFrom0(10); // gets an integer from 0 to 9
*
* @returns {int}
*/
Math.randomFrom0 = function(max)
{
return Math.floor(Math.random() * max);
};
/**
* Returns a random alphanumeric string of the given length. Useful for generating quick "unique" IDs; NOT for
* generating passwords or other things where security is a priority.
*
* @example let saveGameId = Math.randomString(20); // 62^20 = a shit ton; chance of collision is "quite low"
*
* @returns {string}
*/
Math.randomString = function(length)
{
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let text = '';
for(let i = 0; i < length; i++)
text += characters.randomCharacter();
return text;
};
// extending objects in a way that won't break for...in:
Object.defineProperty(String.prototype, 'randomCharacter', {
/**
* Returns a random character from the string.
*
* @example return 'Ben'.randomCharacter(); // returns "B", "e", or "n"
*
* @returns {string}
*/
value: function() {
return this.charAt(Math.randomFrom0(this.length));
}
});
Object.defineProperty(Array.prototype, 'randomIndex', {
/**
* Returns a random array index from the array.
*
* @example return [ 'mangoes', 'watermelons', 'brussel sprouts' ].randomIndex(); // returns 0, 1, or 2
*
* @returns {int}
*/
value: function() {
return Math.randomFrom0(this.length);
},
});
Object.defineProperty(Array.prototype, 'randomElement', {
/**
* Returns a random element from the array.
*
* @example let animal = [ 'fox', 'cat', 'goblin' ].randomElement(); // gets "fox", "cat", or "goblin"
*
* @returns {*}
*/
value: function() {
return this[this.randomIndex()];
}
});
Object.defineProperty(Object.prototype, 'randomProperty', {
/**
* Returns a random property from the object.
*
* @example return { name: 'Ben', favoriteFruit: 'mangoes' }.randomProperty(); // returns "name" or "favoriteFruit"
*
* @returns {string}
*/
value: function() {
return Object.keys(this).randomElement();
}
});
Object.defineProperty(Object.prototype, 'randomValue', {
/**
* Returns a random property value from the object.
*
* @example return { name: 'Ben', favoriteFruit: 'mangoes' }.randomProperty(); // returns "Ben" or "mangoes"
*
* @returns {*}
*/
value: function() {
return this[this.randomProperty];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment