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


i have list of sorted elements. first elements supposed prioritized in contrast last elements. currently, using random.choice(foo) select items list.

the algorithm writing lot more efficient different probability distribution (described above). unfortunately, not sure how implement it

you can use geometric distribution. in geometric distribution probability larger number decreases exponentially. can tweak priority adjusting parameters.

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

in snippet above, x correspond number of bernoulli trials have perform before encounter first success probablity of success if 0.5.

in general, probability distribution return number n equal (1 - pr)^(n-1) * pr pr parameter can see in code.

so should use x-1 (since lists 0-indexed , x >= 1) index choose element in list , since smaller values have higher probability of appearing distribution, have captured requirement :)


Comments