lundi 10 septembre 2018

Random pick 2 numbers with a minimum difference of 5

I have a list of running numbers and I am trying to randomly pick 2 numbers in the list while making sure that the difference between these two numbers is more than 5. Also, the numbers picked cannot be the first or the last 5 numbers of the input list.

I have written this code but it does not work well.

_list = random.sample(range(5, len(_det)-5), 2)

if max(_list) - min(_list) < 5:
    _list = random.sample(range(5, len(_det)-5), 2)
else:
    pass

A number of different lists go through this same code. Some can be as long as 800 running digits and some can be as short as 14. Therefore, if the list is too short, the code should return an error and exit the program.




Aucun commentaire:

Enregistrer un commentaire