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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zavax
  • 59
  • 1
  • 2
  • 11
  • Possible duplicate of [How do I simulate biased die in python?](https://stackoverflow.com/questions/479236/how-do-i-simulate-biased-die-in-python) – SiHa Sep 21 '17 at 06:53
  • That's slightly the point it's supposed to favor survival of the "fittest" but still uses RNG to select an array index. – Zavax Sep 21 '17 at 06:54
  • Yeah, sorry, the comment was a bit snarky. Still, a search for *Python biased random* yields quite a few hits. – SiHa Sep 21 '17 at 06:58
  • @Zavax: Please see my edit for an updated answer – Unni Sep 21 '17 at 07:32

1 Answers1

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]
Unni
  • 5,348
  • 6
  • 36
  • 55