lundi 10 octobre 2016

How to generate random non-recurring numbers in a loop and within a range in python?

Hi, I'm still a beginner and a bit lost. I'm working on a project for school that requires me to write different small programs that will 'guess' the given password. This is a bruteforce program, and I need it to guess every possible combination of 4 number passwords like those on the old iPhones. My problem is that when I use random.sample it generates the same random numbers multiple times. What function can I use, or what should I change so that the random numbers within the given range don't repeat themselves? I tried doing rand.int but it gave me "TypeError: 'int' object is not iterable"

Additional questions: - How do I get my loop to stop once n == Password4 ? It simply continues, even after the correct password is found. - Is there a way I can count the number of fails(n != Password4) before my success (n == Password4)?

This is my code:

    import random

    Password4 = 1234
    def crack_password():

while True:
    for n in (random.sample(range(1112, 10000), 1)):
        while n == Password4:
            print(n, "is the password")
            break
        if n != Password4:
            print('fail')
            break

    crack_password()




Aucun commentaire:

Enregistrer un commentaire