Skip to content

Instantly share code, notes, and snippets.

@jdpaton
Created March 27, 2012 00:09
Show Gist options
  • Save jdpaton/2210886 to your computer and use it in GitHub Desktop.
Save jdpaton/2210886 to your computer and use it in GitHub Desktop.
Generate a random lowercase string with at least one number
python -c "import random; x = [random.choice('23456789') for x in range(1)] + [ random.choice('abcdefghjkmnpqrstwxyz') for x in range(3)]; random.shuffle(x); print ''.join(x)"
@jdpaton
Copy link
Author

jdpaton commented Mar 27, 2012

Oh and only one system binary allowed, in this case I used 'python'.

@jeff-hobbs
Copy link

perl -e 'print map { (split //, 'abcdefghjkmnpqrstuwxyz23456789')[rand 30] } 1..4'

Doesn't handle the guaranteed 1 number case, but you can drop the 'u' (and then rand 29) to guarantee no f* word generation. ;)

@jandubois
Copy link

Your (jdpatons) solution is incorrect, as it includes both 'l' and 'v' :P

@jandubois
Copy link

Jeff: your solution doesn't guarantee that you include a digit.

@jandubois
Copy link

$ perl -MList::Util=shuffle -e 'print shuffle(chr(50+rand(8)),map substr("abcdefghjkmnpqrstuwxyz",rand(22),1),1..3)'

Or, since the "ground rules" don't require the digit to be in a random place:

$ perl -e 'print chr(50+rand(8)),map substr("abcdefghjkmnpqrstuwxyz",rand(22),1),1..3'

@jeff-hobbs
Copy link

Jan: yeah, yeah, I noted that (s/char/number/ edit in my sentence). As the 1 number was specified as a preventative for swear words, I'm willing to take that 1/(30^4) chance on fuck. No shit allowed (no 'i').

@jdpaton
Copy link
Author

jdpaton commented Mar 27, 2012

@jandubois, how silly of me, updated :)

Your (1st) solution does seem to be shorter, unless I can figure out a way to have a regex instead of the two generators, the python one is not going to get much smaller.

@jandubois
Copy link

Not using an external library, and shaving off some characters:

perl -e'print map{splice@_,rand@_,1}@_=(chr 50+rand 8,map substr(abcdefghjkmnpqrstuwxyz,rand 22,1),1..3)'

But now readability is suffering quite a bit... :)

@jandubois
Copy link

Easier to understand and 3 characters shorter:

perl -e'@_=map substr(abcdefghjkmnpqrstuwxyz,rand 22,1),1..3;splice@_,rand@_,1,chr 50+rand 8;print @_'

@jdpaton
Copy link
Author

jdpaton commented Mar 27, 2012

I still need to figure out how to only generate one number and randomise it's position....

python -c"".join(('abcdefghjkmnpqrstqxyz'*12)[ord(x)][0]+('23456789'*50)[ord(x)][0] for x in __import__('os').urandom(2))"

(Note the 'u' is gone too, forgot to mention that character can be dropped)

Jan, it's not easy to see how you are not generating the numeric zero / 1, are you starting the numeric index from > 1? I'm intrigued :)

@jeff-hobbs
Copy link

chr 50 == 2 + [0..7](rand 8). I don't know that we need full readability on this, and the no-external modules perl solution seems fine as it fills all the criteria, even randomizing the digit. I need 1..4 to get 4 chars though. Jan - is that a buglet?

@jandubois
Copy link

@jeff-hobbs: Yes, my latest entry should have 1..4 in it. And dropping the 'u' saves another character:

perl -e'@_=map substr(abcdefghjkmnpqrstwxyz,rand 21,1),1..4;splice@_,rand@_,1,chr 50+rand 8;print @_'

@jandubois
Copy link

Finally, I would like to change @_ to @$ as a hidden reference to ActiveState (@$ is ActiveState similar to how *$ is Starbucks):

perl -e'@$=map substr(abcdefghjkmnpqrstwxyz,rand 21,1),1..4;splice@$,rand@$,1,chr 50+rand 8;print @$'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment