Sinisterly
[Python] Random password generator. - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Python (https://sinister.ly/Forum-Python)
+--- Thread: [Python] Random password generator. (/Thread-Python-Random-password-generator)

Pages: 1 2


[Python] Random password generator. - H4R0015K - 02-19-2013

Code:
import random def passgen(x): char='abcdefghijklmnopqrstuvwxzy' charup='ABCDEFGHIJKLMNOPQRSTUVWXYZ' nums='0123456789' sym='!@#$%^&*()_+-=,<.>/?;:"{[}]|' password='' while len(password)!=x: if random.randrange(0,100)/2==0: password+=char[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=charup[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=nums[random.randrange(0,9)] elif random.randrange(0,100)/2==0: password+=sym[random.randrange(0,28)] return password print(passgen(10))



RE: [Python] Random password generator. - Deque - 02-19-2013

Nice snippet. Did you do it this way to get the different character types with different probabilities?
Otherwise you could just put them all in one array.

I suggest doing this instead of randrange:

Code:
password += random.choice(sym)

It is more clear and you avoid the inflexibility of the hardcoded range, i.e. if you add a character to sym, you don't have to change the max value for randrange. You could also do this by using len(sym), though.


RE: [Python] Random password generator. - H4R0015K - 02-19-2013

(02-19-2013, 01:52 PM)Deque Wrote: Did you do it this way to get the different character types with different probabilities?

Yes.

(02-19-2013, 01:52 PM)Deque Wrote: I suggest doing this instead of randrange:

Code:
password += random.choice(sym)

It is more clear and you avoid the inflexibility of the hardcoded range, i.e. if you add a character to sym, you don't have to change the max value for randrange. You could also do this by using len(sym), though.

Thanks for this info.i was not aware that there is something like random.choice.


RE: [Python] Random password generator. - Ex094 - 02-19-2013

Nice one mate, I'll add this to my code collection Smile


RE: [Python] Random password generator. - diana32 - 03-11-2013

thank for your help Biggrin


RE: [Python] Random password generator. - AdrexX - 03-14-2013

You could make it even more save and user-friendly by saving the password in a txt-file by doing this you could counter keyloggers in stealing you password (Just in case)

The final code would be like this (you could eventually make the program print the password too).

Code:
import random def passgen(x): char='abcdefghijklmnopqrstuvwxzy' charup='ABCDEFGHIJKLMNOPQRSTUVWXYZ' nums='0123456789' sym='!@#$%^&*()_+-=,<.>/?;:"{[}]|' password='' while len(password)!=x: if random.randrange(0,100)/2==0: password+=char[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=charup[random.randrange(0,26)] elif random.randrange(0,100)/2==0: password+=nums[random.randrange(0,9)] elif random.randrange(0,100)/2==0: password+=sym[random.randrange(0,28)] return password #Added Code Goes Here: def doc(x): try: f = open('pwd.txt', 'w') f.write(x) f.close() return True except Exception, e: print e return False if doc(passgen(10)): print "Password Saved in pwd.txt." raw_input() else: raw_input()



RE: [Python] Random password generator. - NoobieXD - 10-23-2013

hey can u tell me where u learn these? Tongue


RE: [Python] Random password generator. - NoobieXD - 10-23-2013

hey can u tell me where u learn these? Tongue


RE: [Python] Random password generator. - NoobieXD - 10-23-2013

hey can u tell me where u learn these? Tongue


RE: [Python] Random password generator. - Deque - 10-23-2013

(10-23-2013, 10:23 AM)NoobieXD Wrote: hey can u tell me where u learn these? Tongue

Read this: http://ictc.aeoi.org.ir/sites/default/files/Gray-Hat-Python.pdf