0

How can I perform selection (i.e. deletion of elements) in an array that tends towards lower numbers.

If I have an array of fitnesses sorted lowest to highest, how can I use random number generation that tends towards the smaller numbers to delete those elements at random.

pop_sorted_by_fitness = [1, 4, 10, 330]

I want to randomly delete one of those smaller elements, where it's most of the time 1, sometimes 4, and rarely 10, with almost never 330. How can I achieve this sort of algorithm.

4
  • Possible duplicate of How do I simulate biased die in python? Commented Sep 21, 2017 at 6:53
  • That's slightly the point it's supposed to favor survival of the "fittest" but still uses RNG to select an array index. Commented Sep 21, 2017 at 6:54
  • Yeah, sorry, the comment was a bit snarky. Still, a search for Python biased random yields quite a few hits. Commented Sep 21, 2017 at 6:58
  • @Zavax: Please see my edit for an updated answer Commented Sep 21, 2017 at 7:32

1 Answer 1

2

How about making use of exponential distribution for sampling your indexes using numpy.random.exponential

import numpy as np 

s = [1, 4, 10, 330] 
limit = len(s)
scale = 10**int(np.log10(limit))
index = int(np.random.exponential()*scale)%limit

Test it

In [37]: sorted([int(np.random.exponential()*scale)%limit for _ in xrange(20)]) 
Out[37]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.