# Generates a random string from a set of easily readable charactersdef generate_activation_code(size = 6)charset = %w{ 2 3 4 6 7 9 A C D E F G H J K M N P Q R T V W X Y Z}(0...size).map{ charset.to_a[rand(charset.size)] }.joinend
class String# generate a random string of length n using current string as the source of charactersdef random(n)return "" if n <= 0(chars * (n / length + 1)).shuffle[0..n-1].joinendend
def generate_random_password(min_length, max_length)length = SecureRandom.random_number(max_length - min_length) + min_lengthcharacter_sets = [('a'..'z').to_a,('A'..'Z').to_a,('0'..'9').to_a,"~!@^&*()_-+=[]|:;<,>.?".split('')]retval = []## Add one character from each set#character_sets.each do |character_set|character = character_set[SecureRandom.random_number(character_set.count)]retval.push characterend## Fill the rest of the password with a random character from a random set#i = character_sets.count - 1while i < lengthcharacter_set = character_sets[SecureRandom.random_number(character_sets.count)]character = character_set[SecureRandom.random_number(character_set.count)]retval.push characteri += 1endretval.shuffle.joinend