Last active
November 14, 2024 15:29
-
-
Save vielhuber/519405489837085bd257 to your computer and use it in GitHub Desktop.
generate a random number between x and y being integers #js
This file contains 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
~~(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