1

I have a lottery game where I can generate 1 set of 6 numbers.I am trying to generate two sets of 6 numbers but am unable to do so.

        public static void LottoDraw()
        {
            int[] lotto = new int[6];
            Random LottoNumbers = new Random();

            for (int i = 0; i < lotto.Length; i++)
            {
                lotto[i] = LottoNumbers.Next(1, 47);
            }
            Array.Sort(lotto);
            Console.WriteLine("Your Lotto Numbers are:");
            for (int i = 0; i < lotto.Length; i++)
                Console.WriteLine(lotto[i]);
            Console.ReadLine();
        }

I tried using a while loop but the variables "line1" and "line2" as seen here Generating 2 Random Numbers that are Different C# but kept generating errors. The method is being called in a switch. The larger set of code can be found here:

static void Main(string[] args)
    {
        int choice = 0;
        do
        {
            DisplayMenu();
            choice = int.Parse(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    LottoDraw();
                    break;

                case 2:
                    EuroDraw();
                    break;

                default:
                    Console.WriteLine("Please enter 1 for Lotto or 2 for EuroMillions");
                    break;
            }
        } while (choice != 2);
    }

    public static void LottoDraw()
    {
        int[] lotto = new int[6];
        Random LottoNumbers = new Random();

        for (int i = 0; i < lotto.Length; i++)
        {
            lotto[i] = LottoNumbers.Next(1, 47);
        }
        Array.Sort(lotto);
        Console.WriteLine("Your Lotto Numbers are:");
        for (int i = 0; i < lotto.Length; i++)
            Console.WriteLine(lotto[i]);
        Console.ReadLine();
    }
  • 1
    Are you trying to do it in the same method? You could easily change that method to return a array of int and just call it twice..... – Sorceri Dec 19 '22 at 21:43
  • What attempts have you made to make *two* sets of numbers? What are the error messages you get? – Immersive Dec 20 '22 at 02:14

3 Answers3

1

Let's shuffle 1 .. 46 numbers and take 6 of them (in order to avoid duplicates). Then we can Join these taken numbers to show on the console:

using System.Linq;

...

// Simplest, but not thread safe
// use Random.Shared if your c# version supports it
private static readonly Random random = new Random();

...

public static void LottoDraw() 
{
    var lotto = Enumerable
      .Range(1, 46)
      .OrderBy(_ => random.NextDouble())  
      .Take(6)
      .OrderBy(item => item)
      .ToArray();

    Console.WriteLine(string.Join(Environment.NewLine, lotto));

    Console.ReadLine();    
}

Fiddle

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • "random does not have a definition for shared" is the error appearing from that answer – DKITStudent Dec 19 '22 at 21:52
  • @DKITStudent: in case you have an *older* version of c# (without `Random.Shared`) you can create `Random` instance manually. Please, see my edit. – Dmitry Bychenko Dec 19 '22 at 21:56
  • still receiving the same error, unfortunately. This method is being called in a switch also, so declaring a separate read-only method will not suffice? I could be wrong, thank you for your feedback – DKITStudent Dec 19 '22 at 22:41
  • Please note that OP already linked to the code that shuffles array (along with explanations why OrderBy is not necessary the best approach), and taking couple items out of the result. I'm not sure why re-iterating mostly the same code is useful... The only part they needed is explanation of two calls to `Take(6)` (which is not even shown in the answer for some reason). – Alexei Levenkov Dec 20 '22 at 02:48
0

Lets write a method which creates lotto sets:

public static List<int> GenerateLottoSet(int n)
{
    var random = new Random();
    return Enumerable.Range(0, n).Select(_ => random.Next(1, 47)).ToList();
}

and then call it as many times as you need:

var set1 = GenerateLottoSet(6);
var set2 = GenerateLottoSet(6);
godot
  • 3,422
  • 6
  • 25
  • 42
  • this still only generates 6 numbers. not two sets of 6? – DKITStudent Dec 19 '22 at 22:10
  • This method is being called in a switch, so the variables need to be declared inside this method? I could be wrong – DKITStudent Dec 19 '22 at 22:39
  • @DKITStudent can you show us the bigger picture? – godot Dec 19 '22 at 22:44
  • updated post for context – DKITStudent Dec 19 '22 at 22:51
  • 2
    @godot, you need to move `var random = new Random();` to OUTSIDE the function so it will only create ONE instance of Random that gets re-used on each call. The way it is now, if you call it in rapid succession (from within a loop, or very close proximity) the current time gets used as the seed and it will generate the same set of numbers. – Idle_Mind Dec 19 '22 at 23:07
-1

I've resolved to use a do while loop that will generate different sets of numbers.

  public static void LottoDraw()
        {
            Console.WriteLine();
            Console.WriteLine("=============================");
            Console.WriteLine();
            Console.WriteLine("How many lines of Lotto do you wish to purchase (minimum 2):");
            Console.WriteLine();
            Console.WriteLine("=============================");
            int lottoLines = int.Parse(Console.ReadLine());
            int currentLine = 2;
            do {
                int[] lotto = new int[6];
                Random LottoNumbers = new Random();

                for (int i = 0; i < lotto.Length; i++)
                {
                    lotto[i] = LottoNumbers.Next(1, 47);
                }
                Array.Sort(lotto);
                Console.WriteLine("Your Lotto Numbers are:");
                for (int i = 0; i < lotto.Length; i++)
                    Console.WriteLine(lotto[i]);
            } while (++currentLine<= lottoLines);
  • Near enough. You seem to have a fascination with ```do-while```, get out of that habit. They have their place but it's very niche. The majority of loops only require a ```for``` (or ```foreach```), infrequently a ```while``` and only very, very rarely a ```do-while```. – Immersive Dec 20 '22 at 02:50
  • You seem to have a fascination on commenting on my particular questions – DKITStudent Dec 20 '22 at 08:50