mercredi 2 juin 2021

How do i sort a random list of 7 numbers out of 35

I want to create a lotto-game and randomly pick 7 different numbers out of 35 numbers in C#. That is what i do in the code but i do not understand how to sort the numbers in ascending order. What i do is to pick out 7 numbers from a list that has 35 numbers and now i am so confused so i need help that at least put me in another way of thinking.lf someone could hlep me with this i would be very glad. I realize that i have messed it up some with the naming. That is (for instance) i create a list (winningNumber) that contains all but the 7 random numbers i want to sort but right now i need help to sort my messy thoughts out. Here is the code.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace Random_Lotto_Number_2021_05_28_SMART
 {
class Program
{
    static void Main(string[] args)
    {
        Random rndLottoNumber = new Random();
        List<int> winningNumber = new List<int>();

        for (int i = 1; i <= 35; i++)
            winningNumber.Add(i);

        for (int i = 0; i < 7; i++)
        {
            int drawnNumber = rndLottoNumber.Next(winningNumber.Count);
            //Console.ReadLine();
            Console.Write(winningNumber[drawnNumber] + ",");
            winningNumber.RemoveAt(drawnNumber);
        }
        Console.WriteLine($"\nThe list contains {winningNumber.Count}");

        foreach(int num in winningNumber)
        {
            Console.Write(num + ",");
        }

        Console.ReadLine();
    }
}
 }



Aucun commentaire:

Enregistrer un commentaire