I am receiving a List<Fruit>
of unkown size, usually between 4-10 items:
{ Apples, Orange, Pear, ?, ?, ... }
How can I weight the List
in a way that Apples
have the highest % chance of being selected, then Orange
, then Pear
?
The effect should essentially be the same as picking a random item from a List
that looks like this:
{ Apples, Apples, Apples, Apples, Orange, Orange, Pear }
If the List
was of a fixed size, I would've done
- Generate a float between 0.0-1.0;
- If
< 0.4
, returnApples
. Else if< 0.75
returnOrange
etc.
Note that this question is not about selecting a random item from a weighted list but about weighting an existing List
of arbitrary length in a way that a random pick will result in an Item's probability of being picked proportional to its position in the List
.