Skip to content

Instantly share code, notes, and snippets.

@ImanMousavi
Last active April 4, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImanMousavi/45ec9a2072883efbf289fe8c8e76e04d to your computer and use it in GitHub Desktop.
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
// 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