Skip to content

Instantly share code, notes, and snippets.

@westc
Last active April 30, 2021 01:54
Show Gist options
  • Select an option

  • Save westc/b74b95269ece2ca7bc7c to your computer and use it in GitHub Desktop.

Select an option

Save westc/b74b95269ece2ca7bc7c to your computer and use it in GitHub Desktop.
Provides a simple way to get a random number in a range or a random value from an array.

JavaScript Snippet - random()

Have you ever had to pick a random value from an array or a range of numbers and wished there was a simple one-liner way of doing it? Me too! The following function will get that done for you:

Parameters

This function will accept zero, one or two arguments:

  1. minOrArray {number | Array} - If a number is given and this is the only argument specified this will be used as the limit value while 0 will be used as the inclusive boundary for the range of numbers that can be returned. If a number is given and limit is not undefined or null this will be the the inclusive boundary for the range of numbers that can be returned. If an array is given a random value from this array will be returned.
  2. limit {number | undefined | null} - If a number is given it will represent the exclusive boundary of the range of numbers from which a random number can be chosen.

Returns

This returns a random number in the cases that no arguments are given, one number argument is given or two number arguments are given. This returns a random value from an array in the case that minOrArray is given as an array.

Example #1 - Get A Random Number Between 0 and 1

The following will produce a random number from 0 (inclusive) to 1 (exclusive):

// HIDE \\
console.evalGistFile('random.js');

// \\
random() // Don't pass any arguments

// \\
random(1) // Only pass one argument

// \\
random(0, 1) // Pass both arguments

Example #2 - Get A Random Number Between 0 and 50

The following will produce a random number from 0 (inclusive) to 50 (exclusive):

// HIDE \\
console.evalGistFile('random.js');

// \\
random(50) // Only pass one argument

// \\
random(0, 50) // Pass both arguments

Example #3 - Get A Random Number Between 5 and 10

The following will produce a random number from 5 (inclusive) to 10 (exclusive):

// HIDE \\
console.evalGistFile('random.js');


// \\
random(5, 10) // Pass both arguments

Example #4 - Get A Random Value From An Array

The following will produce a random value from the given array:

// HIDE \\
console.evalGistFile('random.js');

// \\
words = "the quick brown fox jumps over the lazy dog".split(" ");

// \\
random(words) // Get a random word
function random(minOrArray, limit) {
if (minOrArray && minOrArray.length != undefined) {
var t = Object.prototype.toString.call(minOrArray).slice(8, -1);
if (t != 'Number' && t != 'String') {
t = minOrArray.length;
return minOrArray[random(Math.min(limit || t, t)) | 0];
}
}
if (limit == undefined) {
limit = minOrArray == undefined ? 1 : minOrArray;
minOrArray = 0;
}
minOrArray = minOrArray || 0;
return Math.random() * (limit - minOrArray) + minOrArray;
}
@westc
Copy link
Author

westc commented Apr 29, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment