Answer the question
In order to leave comments, you need to log in
How not to generate a password that has already been generated?
I have this code:
while True:
is_first = False
cs = random.randint(int(col1),int(col2))
symbols = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'
rand = (''.join(choice(symbols) for i in range(cs)))
f = open(f'text.txt', 'a')
f.write(f'{rand}\n')
print(rand)
Answer the question
In order to leave comments, you need to log in
First of all, you don't need to open the file again.
f = open(f'text.txt', 'a')
while True:
is_first = False
cs = random.randint(int(col1),int(col2))
symbols = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'
rand = (''.join(choice(symbols) for i in range(cs)))
f.write(f'{rand}\n')
print(rand)
f.close()
f = open(f'text.txt', 'a')
while True:
is_first = False
cs = random.randint(int(col1),int(col2))
symbols = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'
rand = (''.join(choice(symbols) for i in range(cs)))
if rand in f.read().splitlines():
continue
else:
f.write(f'{rand}\n')
print(rand)
The easiest way to prevent the generated random sequences from repeating is to add them in set().
For example:
import random
def generate_random_massive(col1, col2, size):
results = set()
while len(results) < size:
cs = random.randint(int(col1),int(col2))
symbols = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'
rand = (''.join(random.choice(symbols) for i in range(cs)))
results.update({rand})
return results
if __name__ == '__main__':
col1 = 16
col2 = 16
size = 1000
rands = generate_random_massive(col1, col2, size)
print(rands)
To find "all possible passwords of length N without repeats" from a character set, you can immediately use itertools.product
from itertools import product
symbols = 'abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_'
result = list(product(symbols, repeat = N))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question