14,974,494 members
1.00/5 (1 vote)
See more:
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':
break
print 'bye'```
Posted
Updated 19-Jan-21 15:21pm

## Solution 1

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

print(counter, "iterations")```
v2
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...