S
S
Sheud_z2021-08-26 12:52:46
Python
Sheud_z, 2021-08-26 12:52:46

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)


and I need to somehow not generate a password that is written in text.txt, and when all the options end, for example, all possible password options are written to text.txt, the program stops,

I think for a long time, but I didn’t come up with anything sensible

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem Imaev, 2021-08-26
@AIRC24

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()

secondly, you can:
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)

A
Alan Gibizov, 2021-08-26
@phaggi

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)

But I would not advise calling it passwords and storing it in this form ...

O
o5a, 2021-08-27
@o5a

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))

Returns a list of tuples of the letter sets of each password, which can be used with ''.join() for a complete string.
Just keep in mind that these are large volumes, because. the number of possible passwords will be determined by
len(symbols) to the power of N

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question