Random without repetition?

23.5k Views Asked by At

I want to write a program that displays all the elements of a list in random order without repetition. It seems to me that it should work, but only prints those elements with repetition.

import random

tab = []

for i in range(1, 8):
    item = random.choice(["house", "word", "computer", "table", "cat", "enter", "space"])
    if item not in tab:
        print(item)
    else:
        tab.append(item)
        continue
4

There are 4 best solutions below

0
On

Instead of random.choice within the for loop, use random.shuffle here.

This way, your list is guaranteed to be have all the elements, while also maintaining the requirement of random order:

>>> import random
>>> tab = ["house", "word", "computer", "table", "cat", "enter", "space"]
>>> random.shuffle(tab)
>>> print tab

As for your original code, that will not work, since the if else block as you've written ensures that no element is added within the list tab. You could correct that by removing the else block like below:

>>> for i in range(1, 8):
...     item = random.choice(["house", "word", "computer", "table", "cat", "enter", "space"])
...     if item not in tab:
...         print(item)
...         tab.append(item)
... 
house
cat
space
enter

But now you will need to alter the logic so that the runs in which same value is randomly returned don't affect the number of outputs.

1
On

Read about capabilities of Random library. It can write simpler. For example:

import random

data = ["house", "word", "computer", "table", "cat", "enter", "space"]
x = random.sample(data, len(data))
print(x)
0
On

As Anshul pointed out the use of random shuffle is great:

import random

options = ["house", "word", "computer", "table", "cat", "enter", "space"]
random.shuffle(options)
print (options)

If you however do not want to import the random library it is possible to solve the problem like below:

options_shuffled = []
options = ["house", "word", "computer", "table", "cat", "enter", "space"]
while len(options) > 0:
    random_bytes = open('/dev/urandom', 'rb').read(4)
    idx = int(int.from_bytes(random_bytes, 'big') / 2 ** (8 * 4) * len(options))
    options_shuffled.append(options[idx])
    del options[idx]

print (options_shuffled)
1
On
import random
items = ["house", "word", "computer", "table", "cat", "enter", "space"]
tab= []
while len(tab) < len(items):
    item = random.choice(items)
    if item not in tab:
    tab.append(item)
print(tab)