An algorithm to generate random strings to be used for coupon codes or promotion codes
Ruby Shell
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is an OOP implementation and modified version of the ruby implementation of

From the website above:

 The code generation algorithm avoids 'undesirable' codes.
 For example any code in which transposed characters happen to result in a valid checkdigit will be skipped.
 Any generated part which happens to spell an 'inappropriate' 4-letter word (e.g.: 'P00P') will also be skipped.

This gem does not include algorithmic validation. That's a TODO.


Add this line to your application's Gemfile:

gem 'promocode_generator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install promocode_generator


Usage: generator = 10, prefix: 'CNN') codes = [] 10.times { codes << generator.generate } puts codes


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Benchmarking test results for 100_0000

n = 100_000

h = 10, prefix: 'CNN')
        @symbols_used="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"> { |x| { n.times { h.generate }}}
        user     system      total        real
     3.890000   0.030000   3.920000 (  3.954316)


Even with 7996 retries we can generate 100_000 coupons within 4 seconds

for  1_000_000
      user     system      total        real
  36.730000   0.190000  36.920000 ( 37.350304)
for 10_000_000
      user     system      total        real
378.320000   3.560000 381.880000 (398.113595)

following the pattern above the approximate time for generating 100_000_000 codes would be around an hour and a half


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.