jeudi 13 août 2015

Selecting an element from an array 35% more often

edited

I have a list with N elements, where K elements are "special" and the rest are "normal". What I'm trying to do is pick an element at random, but special elements should be selected 35% more often than normal items.

For example:

var myList = [
   {id: 1, special: 0},
   {id: 2, special: 1} // <= special item
];

After 600 selections, the normal element should be selected 250 times, and the second should be selected 35% more times than that, or 350 times.

This is different from the suggested duplicate question because my weights do not add up to 1. I can have any arbitrary of elements in my list, and zero or more of them are special. The weight is always .35 for special items, and 0.0 for normal items.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire