Bfec5f7d1a4aaafc5a2451be8c42d26a

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 ?

1
2
3
4
5
6
7
8
9
10
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 !

Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 16, 2007, September 16, 2007 20:32, permalink

No rating. Login to rate!

Here's my refactoring. Replaced times loop by inject and also added some random upercase to make it more secure

1
2
3
4
5
6
7
8
9
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
880cbab435f00197613c9cc2065b4f5a

danielharan

September 17, 2007, September 17, 2007 12:51, permalink

2 ratings. Login to rate!

Here's a lazy solution that will return passwords in the same length-range as the original function.

1
2
3
4
# requires apg; see doc for flags
def random_password
  `apg -a0 -n 1 -m8 -x10 -M CL`.chomp
end
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 17, 2007, September 17, 2007 13:59, permalink

No rating. Login to rate!

w00t! half the line count, nice refactoring Daniel! + it's more secure I guess

Avatar

jonathantan86

September 28, 2007, September 28, 2007 00:22, permalink

3 ratings. Login to rate!

Why the true/false or odd/even logic? Just unroll the loop. :-)

1
2
3
4
5
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
Avatar

Pointernil

September 28, 2007, September 28, 2007 01:56, permalink

4 ratings. Login to rate!

v[rand*c.size]
->
v[rand*v.size]
minor one.

1
2
3
4
5
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
22100344f91ad34ad9fb8fb6b6940a84

Vamsee

September 28, 2007, September 28, 2007 03:34, permalink

1 rating. Login to rate!
1
2
3
4
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
Fdc7ff1d176325ed86c81c7c4584f0ad

Dmitry Sabanin

September 29, 2007, September 29, 2007 06:44, permalink

No rating. Login to rate!

I guess refactoring became golfing now :)

Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 29, 2007, September 29, 2007 13:49, permalink

No rating. Login to rate!

Nice jonathantan86 && Pointernil but you don't have any upercase in there. And Vamsee, this is not "pronouncable"

Avatar

john

September 30, 2007, September 30, 2007 18:31, permalink

No rating. Login to rate!

how do you pronounce an uppercase letter macournoyer?

Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

September 30, 2007, September 30, 2007 20:06, permalink

No rating. Login to rate!

You shout it!

7c45f63f61e478233f0c2ad3006b178c

michiel

October 1, 2007, October 01, 2007 13:18, permalink

2 ratings. Login to rate!

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*).

1
2
3
4
5
6
7
8
9
10
11
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
Bfec5f7d1a4aaafc5a2451be8c42d26a

macournoyer

October 1, 2007, October 01, 2007 13:43, permalink

No rating. Login to rate!

Really nice michiel I like your refactoring!

Your refactoring





Format Copy from initial code

or Cancel