I did some googling trying to find a bias or weighted random number generator written in JavaScript but I couldn’t find anything… :(
So I made one :D
My bias random number generator is based off of this one and this is mine:
function randomNumBias(min, max, bias, influence) { var random = Math.random() * (max - min) + min; var difference = random - bias; var mixer = Math.pow(Math.random(), influence); var toBeRemoved = difference * mixer; return random - toBeRemoved; }
Options
min (number): The smallest possible number max (number): The largest possible number bias (number): The number you want to happen more often influence (number): The number that affects how close your random number will be to bias. The larger the influence, the less likely you are to hit the bias. For example, an influnce of 0 means you will get bias 100% of the time.
Below is an example of it in action.
206 comments for “Finally, a bias random number generator”