Skip to content

Instantly share code, notes, and snippets.

@cannikin
Created August 19, 2008 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannikin/6252 to your computer and use it in GitHub Desktop.
Save cannikin/6252 to your computer and use it in GitHub Desktop.
# Generates a random string of letters and numbers
letters = ('A'..'Z').to_a
print 'Length of random string? '; length_of_string = gets.chomp.to_i
print 'How many to make? '; number_to_make = gets.chomp.to_i
numbers = []
while numbers.length < number_to_make do
output = ''
length_of_string.times { rand(2) == 1 ? output += letters[rand(26)].to_s : output += rand(10).to_s }
numbers << output unless numbers.include? output
end
puts numbers.inspect
#
# Sample output
#
# Length of random string? 10
# How many to make? 5
# -> ["6E98YT8336", "D238RU98RO", "I6701D484D", "BWP8EBC2R6", "2634HDD15D"]
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment