mercredi 18 décembre 2019

generate a random range between two numbers

What is the pythonic (can use numpy) way to generate a random range of length [range_length_min, range_length_max] in the range [range_start, range_end]?

Example:

  • range_length_min = 5
  • range_length_max = 10
  • range_start = 0
  • range_end = 2000

Allowed Solutions:

  • [53, 59]
  • [934, 941]

Invalid Solutions:

  • [92, 94] because length of range is less than range_length_min
  • [92, 104] because length of the range is more than range_length_max
  • [-4, 3] because start of range is less than range_start
  • [1998, 2004] because end of range is less than range_end

Current Solution:

start = np.random.randint(range_start, range_end - (range_max_length - range_min_length))
end = start + np.random.randint(range_min_length, range_max_length)

This gives the correct result but does not sample uniformly. The range_end - (range_max_length - range_min_length) is a hack.




Aucun commentaire:

Enregistrer un commentaire