jeudi 20 février 2020

Generating distinct random numbers using import random python

I am wondering how the following code works:

def gen_game():
    rst = set()
    while len(rst) < 4:
        rst.add(random.randint(0, 9))
    print(rst)
    return "".join(str(i) for i in rst)

I understand that the above function will generate random numbers and add that together beside each other. What I am concern is that how do I know if the number do not generate distinct numbers?

For example, using random.randint(0,9). How come I do not get double 9s? or triple 9s? or quadruple 9s?

Thanks in advanc




Aucun commentaire:

Enregistrer un commentaire