HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Calculate odds of winning (UK) Lottery jackpot

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
oddsjackpotwinninglotterycalculate

Problem

I am interested in calculating odds of winning UK Lottery. The format is that 6 numbers from 1-59 are drawn. I am interested only (at this stage) in the odds of winning the jackpot (matching six balls). As an aside, I'm interested in the odds for a total balls count of 49, and 59, to see the change in chance of winning.

The mathematical formula for calculating the odds is (where 49 is total balls, 6 is number drawn:

\$\text{Odds of winning} = \dfrac{49!}{6!*(49-6)!}\$

The main method of my code is to collect input from the user on parameters of the draw.

I have a class called DrawInfo to store information about the draw. I have a simple method to return the Factorial of a number.

I have a method to calculate the odds of winning the jackpot. This is currently all in the one class, as a small, simple app. I do appreciate that DrawInfo could live in its own class.

```
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the total number of balls in the draw: ");
int totalBalls = int.Parse(Console.ReadLine());

Console.WriteLine("enter the number of balls drawn: ");
int ballsDrawn = int.Parse(Console.ReadLine());

DrawInfo di = new DrawInfo(totalBalls, ballsDrawn);

int totalWinOdds = FindJackpotWinningOdds(di);
Console.WriteLine(String.Format("the odds are 1/{0:n0}", totalWinOdds));
Console.ReadLine();
}

static int FindJackpotWinningOdds(DrawInfo di)
{
BigInteger totalBallsFactorialSum = Factorial(di.TotalBalls);
BigInteger ballsDrawnFactorialSum = Factorial(di.BallsDrawn);

BigInteger JackpotWinningOdds = 0;
JackpotWinningOdds = totalBallsFactorialSum / ((ballsDrawnFactorialSum * Factorial((di.TotalBalls - di.BallsDrawn))));
return (int)JackpotWinningOdds;
}

static BigInteger F

Solution

A quick note to start:

Console.WriteLine(String.Format("the odds are 1/{0:n0}", totalWinOdds));


Is exactly the same as:

Console.WriteLine("the odds are 1/{0:n0}", totalWinOdds);


Now on to some maths fun... There's a multiplicative version which means you don't have to compute such massive numbers so don't need to use BigIntegers:

private int GetBinomialCoefficient(int totalNumberOfBalls, int numberOfBallsDrawn)
{
    // range checking of arguments omitted.
    var total = 1;
    for (var i = 1; i <= numberOfBallsDrawn; i++)
    {
        total *= (totalNumberOfBalls + 1 - i) / i;
    }
    return total;
}



GetBinomialCoefficient(59, 6) == 45057474

Edit

I have changed the name of the method away from GetJackpotOdds based on 200_success's excellent answer.

Code Snippets

Console.WriteLine(String.Format("the odds are 1/{0:n0}", totalWinOdds));
Console.WriteLine("the odds are 1/{0:n0}", totalWinOdds);
private int GetBinomialCoefficient(int totalNumberOfBalls, int numberOfBallsDrawn)
{
    // range checking of arguments omitted.
    var total = 1;
    for (var i = 1; i <= numberOfBallsDrawn; i++)
    {
        total *= (totalNumberOfBalls + 1 - i) / i;
    }
    return total;
}

Context

StackExchange Code Review Q#118529, answer score: 11

Revisions (0)

No revisions yet.