Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i'am trying to generate a random password using python random library where i've defined a number of alphabets(upper, lower & digit) in a variable and also defined the password length size should be 3(EXAMPLE), now using if statement i tried to check whether the random generated password would match the defined string in if statement. NOW my question is how do i check how much iteration it takes to match the pattern

What I have tried:

Python
import random
sbc = 'abc'
passlen = 3
passwd = ''.join(random.sample(sbc,passlen))
while 1:
    #NOW HOW DO I COUNT HOW MUCH ITERATION DOES IT TAKES TO MATCH THE STRING
    if passwd == 'abc':
        print 'password matched'
        break
print 'bye'
Posted
Updated 19-Jan-21 15:21pm

1 solution

Python
counter = 0
while 1:
    passwd = ''.join(random.sample(sbc,passlen))
    if passwd == 'abc':
        print('password matched')
        break
    print(passwd, "does not match")
    counter += 1

print(counter, "iterations")
 
Share this answer
 
v2
Comments
CPallini 20-Jan-21 2:11am    
5.
Richard MacCutchan 20-Jan-21 4:10am    
Thanks Carlo. But why now? :)
CPallini 20-Jan-21 4:16am    
I'm a bit retard late...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900