1

I'm making a webshop for school and had a quick question. I'm trying to write a code that generates a random coupon code and it actually works (did some extreme programming in Console Application), but it's simply not efficient.

static void Main(string[] args)
    {
        Random r = new Random();
        string ALphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int size = 4;

        char[] code1 = new char[size]
        char[] code2 = new char[size]
        char[] code3 = new char[size]
        for (int i = 0; i < size; i++)
        {
            code1[i] = Alphabet[r.Next(Alphabet.Length)];
            code2[i] = Alphabet[r.Next(Alphabet.Length)];
            code3[i] = Alphabet[r.Next(Alphabet.Length)];
        }

        string code4 = new string(code1);
        string code5 = new string(code2);
        string code6 = new string(code3);

        Console.WriteLine(code4 + " - " + code5 + " - " + code6);
        Console.ReadLine();
    }

This works.. somehow. But I would like to see it more efficient, because when I want to generate 100 coupons... this isn't really the way to do that.

I did see something on joining strings, use string.Insert to get that " - " in between and loop it multiple times, but I couldn't get a clear tutorial on how to do that with... well this kind of code.

Anyone got a efficient and (preferable) easy solution?

=======

UPDATE!

this does end up in a database eventually

John Saunders
  • 160,644
  • 26
  • 247
  • 397
HenkKaasman
  • 55
  • 1
  • 11
  • 1
    This question might be better suited for [CodeReview](http://codereview.stackexchange.com). – crthompson May 12 '14 at 20:09
  • Plus you might wanna add some check that you're not generating the same coupon code. – Saverio Terracciano May 12 '14 at 20:10
  • 6
    If you're only generating 100 of these, this will complete in the blink of an eye. The console output is likely to be the most expensive part of the code you've shown. Uniqueness is a much more important concern, although given 36^12 possibilities, I'm not sure there'll be many duplicates with 100 in total... – Jon Skeet May 12 '14 at 20:12
  • Check this: http://stackoverflow.com/questions/730268/unique-random-string-generation – Dávid Kaya May 12 '14 at 20:13
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 12 '14 at 20:18
  • A post to consider, and a possible duplicate, is http://stackoverflow.com/questions/11708559/coupon-code-generation – Mike Perrenoud May 12 '14 at 20:29
  • Oh my god. Alphabet don't have numbers... – Only a Curious Mind May 12 '14 at 20:31

2 Answers2

1

You could be using a StringBuilder for this:

    StringBuilder sb = new StringBuilder();

    Random r = new Random();
    string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int size = 16;

    for (var i = 0; i < size; i++)
    {
        sb.Append(Alphabet[r.Next(Alphabet.Length)]);
    }                 

    Console.WriteLine(sb.ToString());

If you want less code you can make use of a GUID and format it.

Guid.NewGuid().ToString("N").Substring(0, 16);

Update, just saw you needed some formatting between each part of the coupon, so I changed it a litle bit:

    StringBuilder sb = new StringBuilder();

    Random r = new Random();
    string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int pieces = 3, pieceSize = 4;

    for (var i = 0; i < pieces; i++)
    {
        if(i != 0)
            sb.Append(" - ");

        for (var j = 0; j < pieceSize; j++)
        {
            sb.Append(Alphabet[r.Next(Alphabet.Length)]);
        }
    }

    Console.WriteLine(sb.ToString());
bobthedeveloper
  • 3,733
  • 2
  • 15
  • 31
  • Hey thanks, just tested yours and it's working smoothly. one thing though, because right now I'm trying to loop it with a do, while, but it keep generating the same code. Got another idea? :) – HenkKaasman May 12 '14 at 20:52
0

Code is not quite good, but for school app will play I guess )

    static string GenerateCoupon(Random r)
    {
        string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int size = 4;

        char[] code1 = new char[size];
        char[] code2 = new char[size];
        char[] code3 = new char[size];
        for (int i = 0; i < size; i++)
        {
            code1[i] = Alphabet[r.Next(Alphabet.Length)];
            code2[i] = Alphabet[r.Next(Alphabet.Length)];
            code3[i] = Alphabet[r.Next(Alphabet.Length)];
        }

        string code4 = new string(code1);
        string code5 = new string(code2);
        string code6 = new string(code3);

        return string.Format("{0}-{1}-{2}", code4, code5, code6);
    }

    static void Main(string[] args)
    {
        Random rnd = new Random();
        for (int i = 0; i < 100;i++ )
            Console.WriteLine(GenerateCoupon(rnd));
         Console.ReadLine();

    }