vendredi 23 avril 2021

Is there a way to exclude a specific integer from being randomly generated? [duplicate]

I'm attempting a problem which asks me to define a func() that generates 4 unique random integers from 2-8 (inclusive). I've got everything working so far, except that I can't figure out if it's even possible to ensure that the next randomly generated integer isn't a repeat from the given range (2-8)

Code so far:

def get_unique_code():
    code_str = ""

    while len(code_str) != 4:
        x = str(random.randint(2,8))
        code_str += x
    return code_str

Expected output: 6842

Got: 6846




Aucun commentaire:

Enregistrer un commentaire