def random_pronouncable_password(size = 4)
c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
v = %w(a e i o u y)
f, r = true, ''
(size * 2).times do
r << (f ? c[rand * c.size] : v[rand * v.size])
f = !f
end
r
end
Refactorings
No refactoring yet !
macournoyer
September 16, 2007, September 16, 2007 20:32, permalink
Here's my refactoring. Replaced times loop by inject and also added some random upercase to make it more secure
def random_pronouncable_password(size = 8)
consonants = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
vowels = %w(a e i o u y)
(1..size).inject('') do |password, index|
char = (index.even? ? consonants[rand * consonants.size] : vowels[rand * vowels.size])
char.upcase! if (rand * (size / 2)).to_i == 1 # Kick some upcase in there
password << char
end
end
danielharan
September 17, 2007, September 17, 2007 12:51, permalink
Here's a lazy solution that will return passwords in the same length-range as the original function.
# requires apg; see doc for flags def random_password `apg -a0 -n 1 -m8 -x10 -M CL`.chomp end
macournoyer
September 17, 2007, September 17, 2007 13:59, permalink
w00t! half the line count, nice refactoring Daniel! + it's more secure I guess
jonathantan86
September 28, 2007, September 28, 2007 00:22, permalink
Why the true/false or odd/even logic? Just unroll the loop. :-)
def random_pronouncable_password(size=4)
c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
v = %w(a e i o u y)
(1..size).map{c[rand*c.size] + v[rand*c.size]}.join('')
end
Pointernil
September 28, 2007, September 28, 2007 01:56, permalink
v[rand*c.size]
->
v[rand*v.size]
minor one.
def random_pronouncable_password(size=4)
c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
v = %w(a e i o u y)
(1..size).map{c[rand*c.size] + v[rand*v.size]}.join('')
end
Vamsee
September 28, 2007, September 28, 2007 03:34, permalink
def randstring(len=10)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
return Array.new(len, '').collect{chars[rand(chars.size-1)]}.join
end
Dmitry Sabanin
September 29, 2007, September 29, 2007 06:44, permalink
I guess refactoring became golfing now :)
macournoyer
September 29, 2007, September 29, 2007 13:49, permalink
Nice jonathantan86 && Pointernil but you don't have any upercase in there. And Vamsee, this is not "pronouncable"
john
September 30, 2007, September 30, 2007 18:31, permalink
how do you pronounce an uppercase letter macournoyer?
michiel
October 1, 2007, October 01, 2007 13:18, permalink
There is duplication in the list of consonants, and the method does uppercase too. Plus, Array should really have *pick* (rails 2.0 has it, but they call it *rand*).
class Array
def pick
at rand(size)
end
end
def random_pronouncable_password(size=4)
v = %w(a e i o u y)
c = ('a'..'z').to_a - v - ['q'] + %w(qu ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr)
(1..size).map{[c.pick, v.pick]}.flatten.map{|x| [x,x.upcase].pick}.join('')
end
macournoyer
October 1, 2007, October 01, 2007 13:43, permalink
Really nice michiel I like your refactoring!
Method that returns a random password that is easily pronouncable.
Taken in the comments of http://snippets.dzone.com/posts/show/2137
Can you Rubyize this ?