Skip to content

Instantly share code, notes, and snippets.

@callumacrae
Created March 29, 2013 12:52
Show Gist options
  • Select an option

  • Save callumacrae/5270637 to your computer and use it in GitHub Desktop.

Select an option

Save callumacrae/5270637 to your computer and use it in GitHub Desktop.
Random number generator.
/**
* Returns random numbers. See individual functions for docs.
*
* This function can be used as a shortcut function: by default, it will call
* rand.int() using the arguments given to it. You can change which method it
* will call by changing the rand.default string to any of the method names.
*/
function rand(length) {
"use strict";
if (this instanceof rand) {
this.length = length;
} else {
return rand[rand.default].apply(this, arguments);
}
}
// Default method to be called when calling rand().
rand.default = 'int';
(function (number, random, floor) {
"use strict";
/**
* Returns an object which the random methods can be called on to get an
* array of random numbers.
*
* rand.array(10).int(5, 10) will return an array of ten random numbers
* between 5 and 10.
*
* rand.array(7, 9).int(5, 10) will return an array of random length
* between 7 and 9 containing random numbers between 5 and 10.
*
* @param length Length. If maxlength specified, minimum length.
* @param maxlength Maximum length of the array.
* @returns rand object. Should only be used to call methods on.
*/
rand.array = function (length, maxlength) {
if (typeof maxlength === 'number') {
length = rand.int(length, maxlength);
}
return new rand(length);
};
/**
* Returns true or false.
*
* @param prob Probability of returning true: default is 0.5.
* @returns {boolean}
*/
rand.bool = function (prob) {
return random() < (prob || 0.5);
};
/**
* Returns a random float. If neither argument is specified, will return a
* random float between 0 and 1.
*
* @param a If b is specified, lower range. Otherwise, upper range where
* lower range is 0.
* @param b Upper range.
* @returns {number} Random float.
*/
rand.float = function (a, b) {
if (typeof a === number) {
return typeof b === number ? random() * (b - a) + a : random() * a;
} else {
return random();
}
};
/**
* Returns a random integer. In neither argument is specified, will return
* either 0 or 1.
*
* @param a If b is specified, lower range. Otherwise, upper range where
* lower range is 0.
* @param b Upper range.
* @returns {number} Random integer.
*/
rand.int = function (a, b) {
if (typeof a === number) {
if (typeof b !== number) {
b = a;
a = 0;
}
return floor(rand.float(a, b + 1));
} else {
return floor(random() * 2);
}
};
/**
* Internal function to return an array of length length containing random
* stuff.
*
* @param length Length of returned array.
* @param func Function to be called to generate randomness.
* @param args Arguments to be given to the function.
* @returns {Array} Array of length length of randomness.
*/
var each = function (length, func, args) {
for (var a = [], i = 0; i < length; i++) {
a[i] = func.apply(this, args);
}
return a;
};
/**
* rand.array.bool() calls this: builds array and calls rand.bool().
*
* @returns {Array} Array of rand.bool() calls.
*/
rand.prototype.bool = function () {
return each(this.length, rand.bool, arguments);
};
/**
* rand.array.float() calls this: builds array and calls rand.float().
*
* @returns {Array} Array of rand.float() calls.
*/
rand.prototype.float = function () {
return each(this.length, rand.float, arguments);
};
/**
* rand.array.int() calls this: builds array and calls rand.int().
*
* @returns {Array} Array of rand.int() calls.
*/
rand.prototype.int = function () {
return each(this.length, rand.int, arguments);
};
})('number', Math.random, Math.floor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment