简体   繁体   中英

Winning lottery odds

I have another question about lottery. I have to make this problem: "You want to participate in the game of chance 6 out of 49 with only one variant and you want to know what chances you will win:category I (6 numbers),category II (5 numbers),category III (4 numbers). Write a application that receive as input data the total number of balls, the number of balls drawn and then prints the chances of winning with an accuracy of 10 decimals if played with a single variant". My question is: What is the formula to calculate this?I try to find that formula but i didn't find it. An example will be 40, 5 and II (5 numbers) and the result is 0.0002659542 or 45, 15 and category III is 0.0000001324.I need to mention i am a beginner. My code is working but just for 6 from 49.

static void Main(string[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());
        int k = Convert.ToInt32(Console.ReadLine());
        string extract = Console.ReadLine();
        int category1 = category(extract);
        switch (category1)
        {
            case 6:
                calculateTheOddsToWin(n, k, extract);
                break;
            case 5:
                calculateTheOddsToWin(n, k, extract);
                break;
            case 4:
                calculateTheOddsToWin(n, k, extract);
                break;
        }

    }
        static void calculateTheOddsToWin(int n , int k , string extract)
    {
        double comb = combination(n, k);
        decimal solution =(decimal)( 1 / comb);
        decimal round = Math.Round(solution,10);
        Console.WriteLine(round);
    }
        static double combination(int n, int k)
        {
            double factN = factorialN(n);
            double factK = factorialK(k);
            double factNK = substractFactorialNK(n, k);
            double combination = factN / (factNK * factK);
            return combination;
        }

        static double factorialN(int n)
        {
            double factorialN = 1;
            for(int i = 1; i <= n; i++)
            {
                factorialN *= i;
            }
            return factorialN;
        }
        static double factorialK( int k)
        {
            double factorialK = 1;
            for (int i = 1; i <= k; i++)
            {
                factorialK *= i;
            }
            return factorialK;
        }
        static double substractFactorialNK(int n, int k)
        {
            double factorialNK = 1;
            int substract = n - k;
            for (int i = 1; i <= substract; i++)
            {
                factorialNK *= i;
            }
            return factorialNK;
        }
        static int category(string extract)
        {
           if(extract == "I")
            {
                return 6;
            }else if(extract == "II")
            {
                return 5;
            }else if(extract == "III")
            {
                return 4;
            }
            else
            {
                return -1;
            }
        }

You need to calculate three numbers:

T: The total number of combinations
W: The number of ways to draw the desired amount of winning numbers
L: The number of ways to draw the desired amount of losing numbers
Then, the answer is W * L / T

Example: 40 numbers, 5 draws, 4 correct:

W = choose(5,4) = 5 (4 winners from 5 possibilities)
L = choose(35,1) = 35 (1 loser from 35 possibilities)
T = choose(40, 5) = 658008 (5 numbers from 40 possibilities)

5 * 35 / 658008 = 0.00265954

Generically:

n = count of numbers
d = count of available winning numbers = draw size
k = count of winning numbers drawn (d, d-1, and d-2 for I, II, III).
W = choose(d, k) (k winners from d possibilities)
L = choose(n-d, d-k) (d-k losers from n-d possibilities)
T = choose(n, d) (d numbers from n possibilities)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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