Skip to content

Instantly share code, notes, and snippets.

@tildebyte
Last active February 27, 2022 03:21
Show Gist options
  • Save tildebyte/f79e6cac6e00764ae32c0ee3ab6dbd0f to your computer and use it in GitHub Desktop.
Save tildebyte/f79e6cac6e00764ae32c0ee3ab6dbd0f to your computer and use it in GitHub Desktop.
ES6 utility functions for Codepen
// Return a value from a given range, which avoids zero, within a given tolerance.
function avoidZero(range, tolerance) {
// Return a random value in the range from `-range` to strictly less than
// `range`, excluding the inner range +/-`tolerance` (and, logically, zero as
// well).
let value = _.random(-range, range)
while (-tolerance < value && value < tolerance) {
value = _.random(-range, range)
}
return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment