Skip to content

Instantly share code, notes, and snippets.

@etoxin
Created May 31, 2016 03:48
Show Gist options
  • Select an option

  • Save etoxin/fc85f967842f31138ca33105abe6f1e6 to your computer and use it in GitHub Desktop.

Select an option

Save etoxin/fc85f967842f31138ca33105abe6f1e6 to your computer and use it in GitHub Desktop.
Random Functions
// 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