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

7
  • 1
    This question might be better suited for CodeReview. Commented May 12, 2014 at 20:09
  • Plus you might wanna add some check that you're not generating the same coupon code. Commented May 12, 2014 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... Commented May 12, 2014 at 20:12
  • Check this: stackoverflow.com/questions/730268/… Commented May 12, 2014 at 20:13
  • 1
    I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented May 12, 2014 at 20:18

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

1 Comment

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? :)
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();

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.