Last active
April 4, 2023 18:02
-
-
Save ImanMousavi/45ec9a2072883efbf289fe8c8e76e04d to your computer and use it in GitHub Desktop.
A function that returns a random number between 1 and 8 with a higher probability of being less than 2
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
// A function that returns a random number between 1 and 8 with a higher probability of being less than 2 | |
function biasedRandom() { | |
// Generate a random number between 0 and 1 | |
let r = Math.random(); | |
// If the number is less than 0.75, return a number between 1 and 2 | |
if (r < 0.75) { | |
return Math.floor(r * 2) + 1; | |
} | |
// Otherwise, return a number between 3 and 8 | |
else { | |
return Math.floor((r - 0.75) * 8) + 3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment