Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active November 14, 2024 15:29
Show Gist options
  • Save vielhuber/519405489837085bd257 to your computer and use it in GitHub Desktop.
Save vielhuber/519405489837085bd257 to your computer and use it in GitHub Desktop.
generate a random number between x and y being integers #js
~~(Math.random()*(y-x+1))+x
Explanation:
- Math.random(): [0, 1)
- Math.random()*(y-x+1): [0, y-x+1)
- (Math.random()*(y-x+1))+x: [x, y+1)
- ~~(Math.random()*(y-x+1))+x: [x, y]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment