Skip to content

Instantly share code, notes, and snippets.

@dominiceden
Created November 27, 2015 10:36
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 dominiceden/cd8ccb3faf417d65ad02 to your computer and use it in GitHub Desktop.
Save dominiceden/cd8ccb3faf417d65ad02 to your computer and use it in GitHub Desktop.
Generate random alphanumeric codes in Ruby and write them to a CSV file. This is useful for generating voucher codes etc.
# You can use this with irb.
require 'csv'
CSV.open("codes.csv", "wb") do |csv| # Open the empty csv file. Make sure you run irb in the directory where the empty codes.csv file is
(1..10000).each do |i| # Run 10,000 times
randAlphaNumeric = Array.new(8){[*"a".."z", *"0".."9"].sample}.join # Generate a random alphanumeric number that's 8 digits long
csv << ["prefix-" + randAlphaNumeric] # Generate a code that begins with prefix- (if required), add the alphanumeric code and write it to the CSV file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment