Design & Download
Your JS Library
in Seconds!!!

Snippet: random()

Written by Christopher West (cwest) on November 07, 2015.
Either generate a random number or retrieve a random item from an array.
function random(opt_ArrOrMinOrMax, opt_max, opt_round) {
  var argCount = arguments.length,
    arg0Type = typeOf(opt_ArrOrMinOrMax);
  if (arg0Type == 'Array') {
    return opt_ArrOrMinOrMax[random(opt_ArrOrMinOrMax.length - 1, true)];
  }
  if (arg0Type == 'Boolean') {
    opt_round = opt_ArrOrMinOrMax;
  }
  else if (typeOf(opt_max, 'Boolean')) {
    opt_round = opt_max;
    argCount = 1;
  }
  if (argCount < 2) {
    opt_max = argCount == 1 ? opt_ArrOrMinOrMax : 1;
    opt_ArrOrMinOrMax = 0;
  }
  var ret = Math.random() * (opt_max - opt_ArrOrMinOrMax) + opt_ArrOrMinOrMax;
  return ret = opt_round ? Math.round(ret) : ret;
}

random(...) API Documentation

Either generate a random number or retrieve a random item from an array.

Parameters

  1. opt_ArrOrMinOrMax {Array|number}:
    Optional. If this is the only value given and is a number this value will be assigned to opt_max and the minimum value will become 0. If this is an array a random value from this array will be returned. If this is not the only value passed it will be used as the minimum random value.
  2. opt_max {number}:
    Optional. If only one parameter was passed this will become that parameter. If no parameter were passed this will become 1. This is upper bound of the value that would be returned.
  3. opt_round {boolean}:
    Optional. Defaults to false. If true and opt_ArrOrMinOrMax is a number the returned number will be a integer between opt_ArrOrMinOrMax and opt_max with both bounds being inclusive. If false and opt_ArrOrMinOrMax is a number the returned number will be a number between opt_ArrOrMinOrMax (inclusive) and opt_max (non-inclusive).

Returns

If opt_arrOrMinOrMax is an array a random value from the array will be returned, otherwise a random number within the given bounds will be returned.

More Information

You may be wondering if it is really helpful to have a random() function available to YourJS but I assure you the answer is yes! This is what you can do with YourJS's random():

var min = -10, max = 10, arr = [2,4,6,8,10,100];

var num1 = YourJS.random();  // Random number between [0, 1)
var num2 = YourJS.random(arr);  // Random item from array
var num3 = YourJS.random(max);  // Random number between [0, 10)
var num4 = YourJS.random(max, true);  // Random integer between [0, 10]
var num5 = YourJS.random(min, max);  // Random number between [-10, 10)
var num6 = YourJS.random(min, max, true);  // Random integer between [-10, 10]

The equivalent of this using Math.random() would be:

var min = -10, max = 10, arr = [2,4,6,8,10,100];

var num1 = Math.random();  // Random number between [0, 1)
var num2 = arr[Math.floor(Math.random() * arr.length)];  // Random item from array
var num3 = Math.random() * max;  // Random number between [0, 10)
var num4 = Math.round(Math.random() * max);  // Random integer between [0, 10]
var num5 = Math.random() * (max - min) + min;  // Random number between [-10, 10)
var num6 = Math.round(Math.random() * (max - min) + min);  // Random integer between [-10, 10]

As you see, using YourJS.random() is shorter in all cases except that in which no arguments are specified. As is normal with YourJS, I will leave it up to you to decide whether or not to include in your snippet build.