简体   繁体   English

为彩票游戏生成2组随机数c#

[英]Generate 2 sets of random numbers for lottery game c#

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.我有一个彩票游戏,我可以在其中生成一组 6 个号码。我正在尝试生成两组 6 个号码,但我无法这样做。

        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.我尝试使用 while 循环,但变量“line1”和“line2”如此处所示Generating 2 Random Numbers that are Different C# but keep 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();
    }

Let's shuffle 1.. 46 numbers and take 6 of them (in order to avoid duplicates).让我们洗牌1.. 46数字并取其中6个(以避免重复)。 Then we can Join these taken numbers to show on the console:然后我们可以Join这些采取的数字显示在控制台上:

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小提琴

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);

I've resolved to use a do while loop that will generate different sets of numbers.我已经决定使用一个 do while 循环来生成不同的数字集。

  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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM