Skip to content

Instantly share code, notes, and snippets.

@benjamw
Last active February 1, 2017 08:14
Show Gist options
  • Save benjamw/dad971a46d5487347421 to your computer and use it in GitHub Desktop.
Save benjamw/dad971a46d5487347421 to your computer and use it in GitHub Desktop.
Weighted Random Number Generator
//http://stackoverflow.com/questions/30492259/get-a-random-number-focused-on-center
// max is non-inclusive
// bellFactor = 1 is normal flat RNG
// bellFactor = 2 is sawtooth shaped
// bellFactor = 4 is basic bell curve
function weightedRandom(max, bellFactor) {
max = parseInt(max, 10);
bellFactor = parseInt(bellFactor || 1, 10);
if (bellFactor < 1) {
bellFactor = 1;
}
var num = 0;
for (var i = 0; i < bellFactor; i++) {
num += Math.random() * (max/bellFactor);
}
return Math.floor(num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment