Created
May 31, 2016 03:48
-
-
Save etoxin/fc85f967842f31138ca33105abe6f1e6 to your computer and use it in GitHub Desktop.
Random Functions
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
| // True or False | |
| const trueOrFalse = () => | |
| Boolean(Math.random() > .5) | |
| // Random Float | |
| const randomFloat = (min, max) => | |
| Math.random() * (max - min) + min | |
| // Random Number | |
| const randomNumber = (min, max) => | |
| Math.floor(Math.random() * (max - min)) + min | |
| // Random Number (Including Max Value) | |
| const randomNumberInclusive = (min, max) => | |
| Math.floor(Math.random() * (max - min + 1)) + min | |
| // Pick a Random Item From an Array | |
| const randomArrayItem = (arr) => | |
| arr[Math.floor(Math.random() * arr.length)] | |
| // Pick a Random Object Property | |
| const randomObjectProperty = (obj) => { | |
| let arr = Object.keys(obj) | |
| return obj[arr[Math.floor(Math.random() * arr.length)]] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment