Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prioritizing first items in a list (Random & Probability Distribution)

I have a list of sorted elements. First elements are supposed to be prioritized in contrast to last elements. Currently, I am just using random.choice(foo) to select items from my list.

The algorithm I am writing would be a lot more efficient with a different probability distribution (described above). Unfortunately, I am not sure how to implement it

like image 383
Philipp Braun Avatar asked Nov 26 '25 04:11

Philipp Braun


1 Answers

You can use the geometric distribution. In a geometric distribution the probability that you get a larger number decreases exponentially. You can tweak your priority by adjusting some parameters.

import numpy
pr = 0.5
x = numpy.random.geometric(pr, 1)[0]

In the snippet above, x will correspond to the number of Bernoulli trials you have to perform before you encounter your first success where the probablity of success if 0.5.

In general, the probability that the distribution will return some number n is equal to (1 - pr)^(n-1) * pr where pr is the parameter as you can see in the code.

So now you should use x-1 (since lists are 0-indexed and x >= 1) as the index to choose an element in the list and since smaller values have a higher probability of appearing from the distribution, we have successfully captured your requirement :)

like image 74
Banach Tarski Avatar answered Nov 27 '25 17:11

Banach Tarski