0

I am working on a project where I want a program that makes a string containing 5 random words from a list. The list contains 5 words and I want all possible combinations where only one word from the list can be used once per string.

import time
import random

lista = ["w1", "w2", "w3", "Vickpix", "siplse"]

database = []

count = 0

while True:
    nu = random.choice(lista) + random.choice(lista) + random.choice(lista) + random.choice(lista) + random.choice(lista)
    if nu in database:
        pass
    else:
        count += 1
        print(count)
        database.insert(1, nu)
  • Does this answer your question? [How do I generate all permutations of a list?](https://stackoverflow.com/questions/104420/how-do-i-generate-all-permutations-of-a-list) – wovano Dec 06 '22 at 15:48

1 Answers1

0

random.sample doc

import time
import random

lista = ["w1", "w2", "w3", "Vickpix", "siplse"]

database = []

count = 0

while True:
    nu = random.sample(lista, 5)
    if nu in database:
        pass
    else:
        count += 1
        print(count)
        database.insert(1, nu)
eya46
  • 1
  • 2
  • the infinite loop will take a long time to get all of the permutations. If you want to get all permutations do not use a random technique. See https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list – William Feb 13 '22 at 17:58