[Solved] How to generate random unique sets of two numbers from a given range in python


You can store the generate x and y in a list. This way you can check if x and y already exist, and if they don’t, add new ones.

l = []
n = 1
while len(l) < 101:
    if not l:
       x = random.randint(0,n-1)
       y = random.randint(0,n-1)
       l.append((x, y))
    else:
       x = random.randint(0,n-1)
       y = random.randint(0,n-1)
       if (x, y) not in l:
           l.append((x, y))
    n += 1

This would give you a unique tuple (x, y) every time it loops.

0

solved How to generate random unique sets of two numbers from a given range in python