-3

I'm writing small program of generating 30000 distinct alphanumeric codes and I want the program to print the count of already generated codes every time it is divisible by 1000. When I run it it does not print it like

1000
2000
3000

and so on. Instead it prints it like this

It does not happen when I step into and go step by step. The following is the code

static void Main(string[] args)
    {
        char[] charSequence = { '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y' };

        Console.WriteLine(charSequence.Length);
        HashSet<string> set = new HashSet<string>();
        while (set.Count < 30000)
        {
            CodeGenerator cg = new CodeGenerator();
            Random rnd = new Random();
            string code = "";
            for (int i = 0; i < 8; i++)
            {
                int charSequenceNumber = rnd.Next(charSequence.Length);
                code += charSequence[charSequenceNumber];
            }
            set.Add(code);
            if (set.Count % 1000 == 0)
            {
                Console.WriteLine(set.Count);
            }

        }
       Console.WriteLine();
        Console.ReadLine();
    }

Am I doing something wrong?

Shiva
  • 20,575
  • 14
  • 82
  • 112
Dzmitry Sevkovich
  • 871
  • 1
  • 8
  • 16
  • 9
    it is **your bug**. Don't create the `Random` class in a loop. Create it only once outside of your loop. – L.B Dec 30 '13 at 22:43
  • … and don't repeat tags in question titles. – Ondrej Tucny Dec 30 '13 at 22:47
  • It's ***always*** your fault, and not the fault of the compiler, operating system, etc. Those are used by millions of people, and chances are that you would not be the first to discover the bug. – John Saunders Dec 30 '13 at 22:53
  • 2
    @L.B. Ok. Got it. It randomizes based on the time, so I, basically, get the same number over and over within the millisecond time frame, and that is why it repeats. Thank you! You should have put it as an answer. – Dzmitry Sevkovich Dec 30 '13 at 22:54
  • @DzmitrySevkovich there are many questions on SO asking the same thing. Instead of me posting an answer, just delete your questtion. – L.B Dec 30 '13 at 23:04

1 Answers1

0

Let's say you have 28999 elements in the set. You add one more, it gets to 29000, you print it. Next round, you generate a new code. Let's say it already exists in the set. So you try to add it, but it isn't added. Count stays at 29000. You print it again. Bam. And again, and again, and again.

Solution: check the return value of set.Add. It tells you if the element was added or not. If it wasn't, then don't print anything.

And yes, as the commenters noted, don't create Random instances in a loop. But it is not the root of your problem. Even if you do better random number generation, repetitions will occur anyway, and your printing bug will persist.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • 1
    That is not the case, because even if I change it to (set.Count % 10 == 0) it will still do the same. The Question was answered by L.B. in comments to the post – Dzmitry Sevkovich Dec 30 '13 at 22:55
  • Changing the random generation will still cause repetitions, maybe only the frequency of the repetitions will change. The problem will still be that you may print the same number multiple times because you don't check if a new element was added or not. Read my 1st paragraph again, the bug is not about the random generation but the printing logic. – fejesjoco Dec 30 '13 at 22:57
  • There are 1099511627776 different possible codes. The chance of getting the same one when you generate only 30000 is miserable, unless you generate it with different Random classes created within the same millisecond. The problem was the Random class creation inside the while loop. When I placed it before the loop, it started to work fine. – Dzmitry Sevkovich Dec 30 '13 at 23:02
  • You decreased the chances of repetition, so you solved a performance issue, but the actual bug is still there in the code. If you solved the printing bug as I said, your code would work, even with the original (bad) random generation problem. – fejesjoco Dec 30 '13 at 23:05