lundi 5 janvier 2015

Using Random() with a Bias

Suppose I have an array



var list = ['a', 'b', 'c', 'd'];


and I want to select one value from List at random. If it is completely random, each will have a 25% chance. Is there a way to select a value at random with a bias like so:



var bias = [0.1, 0.2, 0.1, 0.6];


(bias of course adds up to 1)


So 'a' has a 10% chance of being selected and 'b' has a 20% chance of being selected, etc


Edit: I know I can just modify do var list = ['a', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd', 'd'] and just select a value at random but I am looking for a more efficient way of doing this by just having an array that contains the bias.





Aucun commentaire:

Enregistrer un commentaire