-1

i have this code and i want it randomly generation not in orders like aaaaaaaaaaaaaaaac i want to start randomly exemple: a025b9c87e84d6454 not aaaaaaaaaac can anyone help me with that ?


def printAllKLength(set, k): 
    n = len(set) 
    printAllKLengthRec(set, "", n, k) 

def printAllKLengthRec(set, prefix, n, k):
    if (k == 0) : 
        print(prefix) 
        return

    for i in range(n): 
        newPrefix = prefix + set[i]         
        printAllKLengthRec(set, newPrefix, n, k-1) 

if __name__ == "__main__":
    print("First Test") 
    set1 = ['A', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
    k = 20
    printAllKLength(set1, k)

2 Answers2

0

Here is a possible solution:

from itertools import product
from random import shuffle

k = 3 # this should be 20 in your case
chars = ['A', 'C', 'D', 'E', 'F',
         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

p = list(product(chars, repeat=k))
shuffle(p)

result = map(''.join, p)

print(*result, sep='\n')

Result:

542
718
849
2FA
FFA
940
065
7AD
335
327
...
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
-1
x = 5
s = 'a','b','c',1,2,9,5
res = ["".join(i) for i in itertools.permutations(map(str, s), x]

res will have your list. s has the possible input characters. x is the length of each generated string.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • thats not working, is there any code to generate x lenth specific letters a,b,c,1,2,9,5 generate all possibility randomly – Khalil Mess Nov 02 '20 at 13:24