FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Python random password generator in one line





davidv
So I took a break from some of my projects (I hate doing multiple projects) and I decided play around with Python, you know just recreationally. Anyway, I've never been very good at making passwords so I made a script that did it for me.

Code:
>>> import string
>>> import random
>>> f = lambda x, y: ''.join([x[random.randint(0,len(x)-1)] for i in xrange(y)]); f(list(string.ascii_letters+string.digits), 12)
'NcADTbYzBiEm'
>>> f = lambda x, y: ''.join([x[random.randint(0,len(x)-1)] for i in xrange(y)]); f(list(string.ascii_letters+string.digits), 12)
'v85tQTLd9CXw'
>>> f = lambda x, y: ''.join([x[random.randint(0,len(x)-1)] for i in xrange(y)]); f(list(string.ascii_letters+string.digits), 12)
'lmR0V5FIiJF6'
>>> f = lambda x, y: ''.join([x[random.randint(0,len(x)-1)] for i in xrange(y)]); f(list(string.ascii_letters+string.digits), 12)
'sGS02DNTTZ3i'
>>>


where x is an iterable and y is length of your desired password. Can anyone top that?
davidv
Oh, nevermind. Topped:

Code:
f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(12)


where x is the length of your desired password.

Here's one with punctuation:

Code:
>>> f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(20)


Terrible vars and also doesn't need to be a list:

Code:
f = lambda x: ''.join(string.printable[random.randint(0,len(string.printable)-1)] for i in xrange(x)]); f(20)
jcreus
davidv wrote:
Oh, nevermind. Topped:

Code:
f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(12)


where x is the length of your desired password.

Here's one with punctuation:

Code:
>>> f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(20)


Terrible vars and also doesn't need to be a list:

Code:
f = lambda x: ''.join(string.printable[random.randint(0,len(string.printable)-1)] for i in xrange(x)]); f(20)

Just for fun, I did my version. I ended up in something like this
Code:

import random
import string

a = [x for x in string.ascii_letters+string.punctuation+string.digits if random.random()>0.5]; random.shuffle(a); print ''.join(a)[:10]


Really really dirty code.

10 can be replaced with any number. It is the length of the resulting string.

Joan.
davidv
jcreus wrote:
davidv wrote:
Oh, nevermind. Topped:

Code:
f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(12)


where x is the length of your desired password.

Here's one with punctuation:

Code:
>>> f = lambda x: ''.join([list(string.ascii_letters+string.punctuation+string.digits)[random.randint(0,len(string.punctuation+string.ascii_letters+string.digits)-1)] for i in xrange(x)]); f(20)


Terrible vars and also doesn't need to be a list:

Code:
f = lambda x: ''.join(string.printable[random.randint(0,len(string.printable)-1)] for i in xrange(x)]); f(20)

Just for fun, I did my version. I ended up in something like this
Code:

import random
import string

a = [x for x in string.ascii_letters+string.punctuation+string.digits if random.random()>0.5]; random.shuffle(a); print ''.join(a)[:10]


Really really dirty code.

10 can be replaced with any number. It is the length of the resulting string.

Joan.


Agreed. Very ugly code but I like what you did there. Constructed a list of random characters (50/50 to add to ignore), convert to string then take up to 10. Problem is, your password generator doesn't allow passwords with the same characters to be in one password.

I still like my lambda solution more though. It's "more" clear and reusable, as anonymous functions tend to be.
Fire Boar
Just for fun, here's a ruby version. Pretty sure you could make similar logic in Python, but ruby is just nice.

Code:
(0..9).map{(33+rand(93)).chr}.join


Replace 9 with the number of digits wanted minus 1. The 33 + rand(93) selects a non-space character which can be types on standard keyboards by ordinal value.
remram44
Of course there is always the classic bash version:
Code:
dd if=/dev/urandom bs=3 count=5 2>/dev/null|base64
Related topics
Random page generator
[c++]random number generator doesnt work
Visual Basic Tutorial - Random numbers
Javascript random code generator script needed please
Make an random password..
Problem connecting to MySQL database with PHP [RESOLVED]
Astrology Love
One Click and boom! - Random Topic
JAVA - random name generator
[Java] Wanna help me figure out this error?
Microsoft confirms that XP contains random number generator
Random Password?
Random Topic Generator
Is lottery totally random?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.