-1

I'm currently working on a little lottery game in a console window made with C#.

I added a feature that lets the user randomize the 6 numbers he will have on his ticket but noticed that my random number generator sometimes generates the same number even though you can theoretically only have a number once, so I built this check to see if any number is the same only to notice that I cant go back to randomizing a new list of numbers because the goto function cant go out of a loop (CS0159). Any other way to do this?

Code:

                Console.WriteLine("\nRandomized 6 numbers: ");
                
                // Randomize 6 numbers in for the array randomList
                for (int z = 0; z <= 5; z++)
                {
                SameNum:
                    int rndNum = rnd.Next(1, 50);
                    randomList[z] = rndNum;
                }
                // Once the list is done check if any elements of the array
                // are the same and if yes go back to SameNum to generate a new 
                // List of 6 numbers
                for (int a = 0; a <= 5; a++)
                {
                    for (int b = 0; b <= 5; b++)
                    {
                        while (randomList[a] == randomList[b])
                        {
                            goto SameNum;
                        }
                    }

                }

                // Display the random list if there are no same numbers
                for (int c = 0; c <= 5; c++)
                {
                    Console.WriteLine(randomList[c] + " ");
                }
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
cloudified
  • 13
  • 5
  • 7
    99.9% of the cases, If you're using `goto` in c#, you're doing something wrong. Refactor your code in such a way that you don't need to use `goto` - separate that code to another method, is the easy refactor move and probably the smart one. – Zohar Peled Mar 06 '22 at 14:44
  • Why are you not checking number when you are generating the number ? – dotnetstep Mar 06 '22 at 14:47
  • 2
    You might find this article useful: [The Fisher-Yates Shuffling Algorithm in C# (And Why It Works So Well)](https://exceptionnotfound.net/understanding-the-fisher-yates-card-shuffling-algorithm/) – Andrew Morton Mar 06 '22 at 14:48
  • @dotnetstep Because the list has to be complete to check if any of them are the same. – cloudified Mar 06 '22 at 14:49
  • 1
    @AndrewMorton Yea that was really useful, dont know why I didnt think of this earlier thank you! – cloudified Mar 06 '22 at 14:54
  • 1
    You can goto out of a loop. You cannot, however, goto *into* a loop. – RBarryYoung Mar 06 '22 at 15:02
  • You should code it as it is done in real life: Have a list with all possible items, shuffle that list and take as many items you need from the top. Thats it. There will be no dupes as long as you do not have dupes in the original list. – Sir Rufo Mar 06 '22 at 15:51
  • @ZoharPeled where did you get this number? In order to be wrong 99.9% of the cases, you must have used `goto` at least 1,000 times. Personally I've used it around ~10 times in my whole life, and I dare to say that in most of those cases I've used it correctly. Last time I used `goto` was [2 years ago](https://stackoverflow.com/questions/25796974/binding-source-thread-in-plinq/67188409#67188409). – Theodor Zoulias Mar 06 '22 at 19:22
  • @TheodorZoulias The number isn't the point, I'm not talking about absolute statistics here - The point is that `goto` is most probably the wrong thing to do in c#. I'm not saying it's *always* wrong - in fact, in languages like basic and visual basic it's very useful (Not vb.net, though) - but working with .net since 1.1 version (vb.net and c#), I didn't find even one case where it was useful to me. – Zohar Peled Mar 07 '22 at 06:49
  • @ZoharPeled [Is using a 'goto' statement bad?](https://stackoverflow.com/questions/11906056/is-using-a-goto-statement-bad) might be an interesting reading. There is no reference there though about the reason I used `goto` the last time I did. It was because of the C# limitation (for good reasons) of not allowing `yield return` inside the body of a `lock` statement. – Theodor Zoulias Mar 07 '22 at 07:30
  • @TheodorZoulias I'm not saying it's bad to use `goto` in all situations. There's a reason why c# has that capability - and sometimes it is the correct solution to a problem. What I'm saying is that most of the times, you don't need to use it. Basically I agree with Jon's answer on the question you've linked. – Zohar Peled Mar 07 '22 at 07:38

1 Answers1

0

Possible solution.

 List<int> digits = new List<int>();
                int z= 0;
                while(z <= 5)
                {
                    int rndNum = rnd.Next(1, 50);
                    if(!digits.Contains(rndNum))
                    {
                       digits.Add(rnbNum); 
                      z++; 
                    }  
                }
    int[] randomList = digits.ToArray();
    digits.Clear();
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • Hey, so I think I know how this works but Im still gonna ask if im right. This code creates a list called digits, then makes a while loop starting at 0 and generating a random number between 1 and 49. Then it checks if digits constains the rolled number. If not it adds the generated number to the list and adds z++ (Indicates that the first number is done). If digits DOES contain the generated number it doesnt add z++ and generates a new number. After it generated 5 different numbers it converts the list to an array and sets it to randomList? Is that what the code does? Thanks ^^ – cloudified Mar 06 '22 at 15:35
  • Also how would I remove all the numbers in the list after its done with setting ```randomList``` to ```digits.ToArray();``` so I can play again because when you play again the numbers rolled are still stored in the list so you can never get them again. – cloudified Mar 06 '22 at 15:42
  • digits.Clear().... – dotnetstep Mar 06 '22 at 15:47