title: Coupon code generation
body: <p>I would like to generate coupon codes , e.g. <code>AYB4ZZ2</code>. However, I would also like to be able to mark the used coupons and limit their global number, let's say <code>N</code>. The naive approach would be something like <em>"generate <code>N</code> unique alphanumeric codes, put them into database and perform a db search on every coupon operation."</em></p>
  
  <p>However, as far as I realize, we can also <strong>attempt to find a function</strong> <code>MakeCoupon(n)</code>, which converts the given number into a coupon-like string with predefined length.</p>
  
  <p><em>As far as I understand, <code>MakeCoupon</code> should fullfill the following requirements:</em></p>
  
  <ul>
  <li><p>Be bijective. It's inverse <code>MakeNumber(coupon)</code> should be effectively computable.</p></li>
  <li><p>Output for <code>MakeCoupon(n)</code> should be alphanumeric and should have small and <strong>constant</strong> length - so that it could be called <em>human readable</em>. E.g. <code>SHA1</code> digest wouldn't pass this requirement.</p></li>
  <li><p>Practical uniqueness. Results of <code>MakeCoupon(n)</code> for every natural <code>n &lt;= N</code> should be totally unique or unique in the same terms as, for example, <code>MD5</code> is unique (with the same extremely small collision probability).</p></li>
  <li><p><em>(this one is tricky to define)</em> It shouldn't be obvious how to enumerate all remaining coupons from a single coupon code - let's say <code>MakeCoupon(n)</code> and <code>MakeCoupon(n + 1)</code> should visually differ.</p>
  
  <blockquote>
    <p>E.g. <code>MakeCoupon(n),</code> which simply outputs <code>n</code> padded with zeroes would fail this requirement, because <code>000001</code> and <code>000002</code> don't actually differ "visually".</p>
  </blockquote></li>
  </ul>
  
  <h2>Q:</h2>
  
  <p><strong>Does any function or function generator, which fullfills the following requirements, exist?</strong> My search attempts only lead me to <a href="http://search.cpan.org/dist/Algorithm-CouponCode/lib/Algorithm/CouponCode.pm" rel="noreferrer"><code>[CPAN]</code> CouponCode,</a> but it does not fullfill the requirement of the corresponding function being bijective.</p>