Home > OS >  Lottery program that generates 6 random numbers between 1 and 59 C#
Lottery program that generates 6 random numbers between 1 and 59 C#

Time:10-28

I have created a simple program which randomly generates 6 winning numbers. While the program works, I would also like for it to ensure that the same number isn't outputted twice as well as sorting them into numerical order when outputted. How would I go about doing such a thing while sticking to similar techniques already used? My code is down below. Any help is very much appreciated.

        int temp;
        int[] lotto = new int[6];

        Random rand = new Random();

        for (int i = 0; i < 6; i  )
        {
            temp = rand.Next(1, 59);
            lotto[i] = temp;
        }
        Console.Write($"The lotterry winning numbers are: ");

        for (int i = 0; i < 6; i  )
        {
            Console.Write(lotto[i]   " ");
        }
        Console.ReadKey();

CodePudding user response:

You can use Linq to create sequence [1..59] and order it by random to shuffle it.

        Random rand = new Random();
        var winners = Enumerable.Range(1, 59)
            .OrderBy(x => rand.Next())
            .Take(6)
            .OrderBy(x => x)
            .ToList();

        Console.WriteLine(String.Join(" ", winners));

CodePudding user response:

Based on a Fisher-Yates shuffle, but saves some work because we know we don't need all the values (if we only need 6 values out of 10 million potentials, we only need to take the first six iterations of the fisher-yates algorithm).

public IEnumerable<int> DrawNumbers(int count, int MaxNumbers)
{
    var r = new Random(); //ideally, make this a static member somewhere
    var possibles = Enumerable.Range(1, MaxNumbers).ToList();
    for (int i = 0; i < count; i  )
    {
        var index = r.Next(i, MaxNumbers);
        yield return possibles[index];
        possibles[index] = possibles[i];
    }
}

var lottoNumbers = DrawNumbers(6, 59);
Console.Write("The lotterry winning numbers are: ");
Console.WriteLine(string.Join(" ", lottoNumbers.OrderBy(n => n)));

See it work here:

https://dotnetfiddle.net/NXYkpU

CodePudding user response:

You will need to add import statement and a bit changes

using System.Linq;

        int temp;
        int[] lotto = new int[6];

        Random rand = new Random();
         
        int i = 0;
        while(i < 6)
        {
            temp = rand.Next(1, 59);
            //check if lotto contains just generated number, if so skip that number
            if (lotto.Contains(temp))
                continue;
            lotto[i] = temp;
            i  ;
        }
        Console.Write($"The lotterry winning numbers are: ");

        // Sort array in ascending order.
        Array.Sort(lotto);
        for (int j = 0; j < 6; j  )
        {
            Console.Write(lotto[j]   " ");
        }
        Console.ReadKey();

CodePudding user response:

I would probably do it Dmitri's way because it is quick and obvious and performance isn't that important with an array this size.

But just for fun, this is slightly more efficient.

IEnumerable<int> GetNumbers(int min, int max, int count)
{
    var random = new Random();
    var size = max - min   1;
    var numbers = Enumerable.Range(min, size).ToArray();
    while (count > 0)
    {
        size--;
        var index = random.Next(0, size);
        yield return numbers[index];
        numbers[index] = numbers[size];
        count--;
    }
}

This solution creates an array containing all possible values and selects them randomly. Each time a selection is made, the array is "shrunk" by moving the last element to replace the element that was chosen, preventing duplicates.

To use:

var numbers = GetNumbers(1, 59, 6).ToList();
foreach (var number in numbers.OrderBy(x => x))
{
    Console.WriteLine(number);
}
  • Related