How do I guarantee unique numbers on a list of lists?

Asked

Viewed 40 times

0

Good evening, I’m trying to make a 5x5 bingo card, using Andom to generate the values, however there can be no equal numbers. I tried to do it that way but it still comes up equal numbers:

import random as rd
cart1 = []

for i in range(0, 5):
    cart1.append([])

for l in range(0, 5):
    for c in range(0, 5):
        var = rd.randint(0, 99)

        if var not in cart1:
            cart1[l].append(var)

print("\n")
print("A cartela 1 é:")
for l in range(0, 5):
    for c in range(0, 5):
        print(f"[{cart1[l][c]:^5}]", end="")

    print()

One of the results:

1 answer

0


import random
lista = random.sample(range(100),10)

This command generates a list of 10 numbers ranging from 0 to 99. That way you can organize it the way you think best.

If you want to organize in array, I recommend using numpy array.

For example:

import random
lista = random.sample(range(100),25)
lista = np.array(lista)
lista = np.reshape(lista, (5, 5))

outworking:

array([[29, 85,  6, 40, 14],
       [78,  2, 77, 41, 46],
       [ 9, 52, 68, 19, 97],
       [65, 90, 75, 93, 50],
       [31,  4, 27, 55, 10]])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.