In translating the formula for calculating lottery odds for various
conditions into a Visual Basic Program, I have apparently missed something
in that I get errors in the part of the calculation where the number of ways
of failure (pFal) is calculated
Both errors happen in the code section
x1 = Draw - MatchesReq
x2 = Field - Selections
For Counter = 1 To (Draw - MatchesReq - 1)
x1 = x1 * (Draw - MatchesReq - Counter)
x2 = x2 * (Field - Selections - Counter)
Next
pFal = x2 / x1
The first type of error is created when the number of matches required is
equal to the number of balls drawn.
This makes x1 = 0 so when it hits the calculation pFal a divide by zero
occurs.
The second type of error is created when the number of balls selected is
equal to the field.
This makes x1 = 0 so when it hits the calculation pFal a divide by zero
occurs.
If anyone can spot my error I would greatly appreciate it.
Lisa
==== SOURCE ======
Public Function CalOdds(Field, Draw, Selections, MatchesReq)
'=================================================
' Field......Number of balls in selection pool
' Draw .....Number of balls drawn in the game
' Selections.......Number of selections made
' MatchesReq......Number of selections that must match
'=================================================
On Error GoTo ErrorExit
Dim x1 ' Temporary working buffer
Dim x2 ' Temporary working buffer
Dim pSel ' Total number of ways of selecting
objects
Dim pSuc ' Total number of ways of getting successes
Dim pFal ' Total number of ways of getting failures
Dim Counter ' Counter used in calculations loops
'
' Calculate the three main factors needed for the final calculation
' First: Calculate total number of ways of selecting objects
'
x1 = Draw
x2 = Field
For Counter = 1 To (Draw - 1)
x1 = x1 * (Draw - Counter)
x2 = x2 * (Field - Counter)
Next
pSel = x2 / x1
'
' Second: Calculate ways to get success
'
x1 = MatchesReq
x2 = Selections
For Counter = 1 To (MatchesReq - 1)
x1 = x1 * (MatchesReq - Counter)
x2 = x2 * (Selections - Counter)
Next
pSuc = x2 / x1
'
' Third: Calculate ways to get failure
'
x1 = Draw - MatchesReq
x2 = Field - Selections
For Counter = 1 To (Draw - MatchesReq - 1)
x1 = x1 * (Draw - MatchesReq - Counter)
x2 = x2 * (Field - Selections - Counter)
Next
pFal = x2 / x1
'
' Do final calculation
'
CalOdds = pSel / (pSuc * pFal)
Exit Function
'
' Sub Routine
'
ErrorExit:
CalOdds = "Error"
End Function
==== FORMULA ======
Assume we have F objects from which to choose. (For Powerball, this would be
the 49 white balls.) Of these F, M are successes (numbers that match the
ones on your ticket, or 5 in the case of Powerball) and F-M are failures
(for Powerball, the 42 balls in the machine that don't match a number on
your ticket). Next, we conduct n Bernoulli trials - we draw n balls (5 for
Powerball) from the machine. What we need to know is the probability of
getting p successes and n-p failures in those n trials. To match all five
white balls, for example, you calculate the probability of 5 successes and 0
failures in 5 trials. The general formula for probability is (Equation 1):
(number of ways of getting p successes) x (number of ways getting n-p
failures)
---------------------------------------------------------------------------
------------------------------------------
(total number of ways of selecting objects from a set of F
Each of these terms boils down to counting. To calculate the denominator,
begin by realizing that there are F ways of selecting the first object, F-1,
ways of selecting the second object, and so on to F-n ways of selecting the
nth object. The total number of ways of making this selection, therefore, is
F(F-1)(F-2)...(F-n). However, in our case the order of selection does not
matter -- (1,2,3,4,5) is the same as (5,4,3,2,1). We need to adjust for the
number of combinations that are identical save for order.
To see this, imagine a drawing of two balls from a set of three. There are 3
ways of picking the first ball and two ways of picking the second, for a
total of 6 outcomes: (1,2), (1,3), (2,1), (2,3), (3,1), and (3,2). However,
only three of these are distinct: (1,2), (1,3), and (2,3) -- the others are
merely reorderings. In general, if we're picking n objects, there will be
(n)(n-1)(n-2)... ways of arranging each unique combination, so we need to
divide our first calculation by this term. There's a mathematical shorthand
for this, called a "binomial coefficient." Using a binomial coefficient, the
total number of ways of selecting n objects from a set of F is:
F!
------------
n!(F-n)!
where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1.