Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 30, 2018 22:57
Show Gist options
  • Save sbrl/65b4f778d91ea81eeee5 to your computer and use it in GitHub Desktop.
Save sbrl/65b4f778d91ea81eeee5 to your computer and use it in GitHub Desktop.
A simple 3 formed bounded random number generator. #function #microlibrary #random
/**
* Bounded random number generator. Has 3 forms:
*
** Form 1 **
* @param {number} a The minimum value.
* @param {number} b The maximum value.
* @param {boolean} c Whether the resulting number should be a float. [optional]
* @return {number} A random number.
*
** Form 2 **
* @param {number} a The maximum value.
* @param {boolean} b Whether the resulting number should be a float. [optional]
* @return {number} A random number.
*
** Form 3 **
* @return {number} A random number.
*/
function random(a,b,c,d)
{d=Math.random();if(typeof a!="number")return d;a=typeof b=="number"?d*(a-b)+b:d*a;a=(typeof b!="number"?b:c)?a:Math.floor(a);return a;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment