Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick an item by its probability?

I have a list of items. Each of these items has its own probability.

Can anyone suggest an algorithm to pick an item based on its probability?

like image 682
Ruzanna Avatar asked Feb 17 '12 14:02

Ruzanna


People also ask

How do you choose elements from a list with different probabilities?

Relative weights to choose elements from the list with different probability. First, define the probability for each element. If you specified the probability using the relative weight, the selections are made according to the relative weights. You can set relative weights using the weight parameter.

How do you find probability in C++?

Probability = total number of K present / size of array. First, count the number of K's and then the probability will be the number of K's divided by N i.e. count / N. Below is the implementation of the above approach: C++


1 Answers

  1. Generate a uniformly distributed random number.
  2. Iterate through your list until the cumulative probability of the visited elements is greater than the random number

Sample code:

double p = Math.random(); double cumulativeProbability = 0.0; for (Item item : items) {     cumulativeProbability += item.probability();     if (p <= cumulativeProbability) {         return item;     } } 
like image 189
Brent Worden Avatar answered Oct 09 '22 22:10

Brent Worden