Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

jon http://burningbush.us

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

generate a random password

I recently had need to generate random passwords for users. Obviously they need to be alphanumerica. Here's what I came up with.

   1  
   2  def newpass( len )
   3      chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
   4      newpass = ""
   5      1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
   6      return newpass
   7  end


It generates a random password using the upper and lower case alphabet, and the numbers 0 to 9, to the size passed in.

No effort is made to make an easy to read password. For example, 0 (zero) _will_ show up next to O (capital o). Yay.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS