Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When generating voucher codes - you should consider whether having a sequence which is predictable is really what you want. </p> <p>For example, Voucher Codes: ABC101, ABC102, ABC103 etc are fairly predictable. A user could quite easily guess voucher codes. </p> <p>To protect against this - you need some way of preventing random guesses from working. </p> <p>Two approaches: </p> <ul> <li><p>Embed a checksum in your voucher codes. </p> <p>The last number on a credit card is a checksum (Check digit) - when you add up the other numbers in a certain way, lets you ensure someone has entered a number correctly. See: <a href="http://www.beachnet.com/~hstiles/cardtype.html" rel="noreferrer">http://www.beachnet.com/~hstiles/cardtype.html</a> (first link out of google) for how this is done for credit cards. </p></li> <li><p>Have a large key-space, that is only sparsely populated.</p> <p>For example, if you want to generate 1,000 vouchers - then a key-space of 1,000,000 means you should be able to use random-generation (with duplicate and sequential checking) to ensure it's difficult to guess another voucher code. </p></li> </ul> <p>Here's a sample app using the large key-space approach: </p> <pre><code> static Random random = new Random(); static void Main(string[] args) { int vouchersToGenerate = 10; int lengthOfVoucher = 10; List&lt;string&gt; generatedVouchers = new List&lt;string&gt;(); char[] keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890".ToCharArray(); Console.WriteLine("Vouchers: "); while(generatedVouchers.Count &lt; vouchersToGenerate) { var voucher = GenerateVoucher(keys, lengthOfVoucher); if (!generatedVouchers.Contains(voucher)) { generatedVouchers.Add(voucher); Console.WriteLine("\t[#{0}] {1}", generatedVouchers.Count, voucher); } } Console.WriteLine("done"); Console.ReadLine(); } private static string GenerateVoucher(char[] keys, int lengthOfVoucher) { return Enumerable .Range(1, lengthOfVoucher) // for(i.. ) .Select(k =&gt; keys[random.Next(0, keys.Length - 1)]) // generate a new random char .Aggregate("", (e, c) =&gt; e + c); // join into a string } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload