Last active
February 1, 2017 08:14
-
-
Save benjamw/dad971a46d5487347421 to your computer and use it in GitHub Desktop.
Weighted Random Number Generator
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
//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