lundi 28 septembre 2020

Optimize randomization of a number excluding a list of values in Python

I need to generate a random number between a min and a max. This number should be a multiple of 10. This is the solution I have come to.

import random
BLOCK = 10

def random_excluding_values(_min, _max, exclude_list):
while True:
    random_number = round(random.randrange(_min, _max - BLOCK) / BLOCK) * BLOCK
    if random_number not in exclude_list:
        return random_number

There is a way to obtain the same result using list list comprehension or something else?




Aucun commentaire:

Enregistrer un commentaire