Created
November 27, 2015 10:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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