Created
July 20, 2011 06:12
-
-
Save angus-c/1094442 to your computer and use it in GitHub Desktop.
Wacky Randomizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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