Skip to content

Instantly share code, notes, and snippets.

@angus-c
Created July 20, 2011 06:12
Show Gist options
  • Select an option

  • Save angus-c/1094442 to your computer and use it in GitHub Desktop.

Select an option

Save angus-c/1094442 to your computer and use it in GitHub Desktop.
Wacky Randomizer
/*
Iterates every Math function in random order, with random arguments,
then randomly adds or subtracts each result and rounds the grand total.
Typical output:
+ tan(9)
+ sqrt(0)
- sin(0)
+ random()
+ round(6)
- min(8, 1)
+ pow(3, 9)
+ max(9, 2)
+ log(1)
+ floor(4)
- exp(6)
+ cos(5)
+ ceil(9)
+ atan2(10, 9)
- atan(1)
+ abs(1)
---------------------
19308
*/
function generateRandomNumber() {
function usefulNumber(value) {
return (typeof value == "number") &&
!isNaN(value) &&
value != Number.POSITIVE_INFINITY &&
value != Number.NEGATIVE_INFINITY;
}
var mathFunctions = Object.getOwnPropertyNames(Math).filter(function(prop) { return typeof Math[prop] == 'function' });
var shuffledFunctions = mathFunctions.sort(function() { return Math.floor(Math.random()*2.999) -1 });
var result = 0, i = shuffledFunctions.length, symbol = "1st";
while(i--) {
var fn = shuffledFunctions[i], first = true;
var l = Math[fn].length, args = [];
while(l--) {
args.push(Math.round(Math.random()*10));
}
var thisResult = (Math[fn]).apply(Math, args);
if(usefulNumber(thisResult)) {
(Math.round(Math.random()) || symbol == "1st") ?
(result += thisResult, symbol = "+") :
(result -= thisResult, symbol = "-");
var output = symbol + " " + fn + "(" + args.join(', ') + ")";
console.log(output);
}
}
console.log("---------------------");
return Math.round(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment