473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error when calculating probablity

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.


Jan 2 '07 #1
10 3761
"Lisa" <so*****@earthlink.net>'s wild thoughts were released
on Tue, 02 Jan 2007 04:42:52 GMT bearing the following
fruit:
>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
Haven't you just identified the error? Divide by zero? If
zero is a possibility then you should always check for it in
code.

J
Jan Hyde (VB MVP)

--
So I was in Tesco's and I saw this man and woman wrapped in a barcode.
I said "Are you two an item?"

Jan 2 '07 #2

"Lisa" <so*****@earthlink.netwrote in message
news:gp***************@newsread4.news.pas.earthlin k.net...
>
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.
If x1 = 0 then
' handle pFal differently
Else
pFal = x2 / x1
End If
Jan 2 '07 #3
"Jan Hyde" <St***********@REMOVE.ME.uboot.comwrote in message
news:0a********************************@4ax.com...
"Lisa" <so*****@earthlink.net>'s wild thoughts were released
on Tue, 02 Jan 2007 04:42:52 GMT bearing the following
fruit:
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

Haven't you just identified the error? Divide by zero? If
zero is a possibility then you should always check for it in
code.

Jan Hyde (VB MVP)
Hi Jan
The problem is zero should not be a possibility and it is occurring because
I have not properly converted this part of the math formula to code.
I have talked to some of the math teachers but they have not a clue as to
how it should be written in VB coding.
This is going to take someone that is knowledgably in both probability math
and VB.
Thanks for the response
Lisa


Jan 2 '07 #4

"Raoul Watson" <Wa*****@IntelligenCIA.comwrote in message
news:2Xpmh.673$Am5.444@trndny03...
>
"Lisa" <so*****@earthlink.netwrote in message
news:gp***************@newsread4.news.pas.earthlin k.net...

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.

If x1 = 0 then
' handle pFal differently
Else
pFal = x2 / x1
End If
Hi Raoul
The problem is not trapping the error.
The problem is that the error should not occur as zero should not be a
possibility and it is occurring because
I have not properly converted this part of the math formula to code.
I have talked to some of the math teachers but they have not a clue as to
how it should be coded in VB.
This is going to take someone that is knowledgably in both probability math
and VB.
Thanks for the response
Lisa
Jan 2 '07 #5
"Lisa" <so*****@earthlink.net>'s wild thoughts were released
on Tue, 02 Jan 2007 16:32:19 GMT bearing the following
fruit:
>"Jan Hyde" <St***********@REMOVE.ME.uboot.comwrote in message
news:0a********************************@4ax.com.. .
>"Lisa" <so*****@earthlink.net>'s wild thoughts were released
on Tue, 02 Jan 2007 04:42:52 GMT bearing the following
fruit:
>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

Haven't you just identified the error? Divide by zero? If
zero is a possibility then you should always check for it in
code.

Jan Hyde (VB MVP)
Hi Jan
The problem is zero should not be a possibility and it is occurring because
I have not properly converted this part of the math formula to code.
I have talked to some of the math teachers but they have not a clue as to
how it should be written in VB coding.
This is going to take someone that is knowledgably in both probability math
and VB.
Thanks for the response
Lisa
Then the question is where are you getting the values for
'Draw' and 'MatchesReq'?

Regardless, it's still wise to check for zero even if it
'shouldn't' happen.


Jan Hyde (VB MVP)

--
Arch Criminal: One who robs shoe stores (Stan Kegel)

Jan 2 '07 #6
The problem is zero should not be a possibility and it is
occurring because I have not properly converted this part
of the math formula to code.
Are you trying to implement this formula which you posted in your first
message?

F!
------------
n!(F-n)!
where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1.

If so, you can add the BigFactorial function I show below to your project
and then just write the formula directly. That is, the above formula would
become this in code...

BigFactorial(F) / (BigFactorial(n) * BigFactorial(F - n))

Note that there is some important discussions included with the BigFactorial
function, so you should read it carefully. While I don't say so in the text
below, you should be aware that your calculations will become meaningless if
the factorial is returned in exponential format due to the loss of accuracy
inherent in having to shove large numbers into that format.

Rick

Below is a combination of two posts I given in the past to someone who asked
about implementing a Factorial function...

You could cast (you can't Dim) a Variant variable as a Decimal type (96-bit
number) and get some 28 or 29 digits of accuracy depending if there is a
decimal in the answer or not. Simply Dim a variable as Variant and CDec a
number (any number will do) into it to make it the Decimal type. Thereafter,
that variable will track 28/29 digits of accuracy. For example the following
function will calculate factorials up to 29 digits of display before
reverting to exponential display.

Function BigFactorial(ByVal N As Integer) As Variant
If N < 28 Then
BigFactorial = CDec(1)
Else
BigFactorial = CDbl(1)
End If
For x = 1 To N
BigFactorial = x * BigFactorial
Next
End Function

However, you have to watch out for overflows with Decimal data types -- once
over 28/29 characters, they will produce an overflow error. So, if you tried
to use the above function like this

Debug.Print 10*BigFactorial(27)

you would get an overflow error but

Debug.Print 10*BigFactorial(28)

would work fine (the difference being in the first case BigFactorial has a
Decimal subtype and in the second case the subtype is a Double).

More generally, if a Variant variable is assigned a value that was cast to
Decimal, any calculation involving that variable will be "performed" as a
Decimal; and then the result cast back to the variable receiving it. If the
result is assigned back to the variable that was originally cast to Decimal,
that variable continues to contain a Decimal type value. For example,

X = CDec(135.6)
X = X - 135
X = X / 7
Print X == 0.0857142857142857142857142857

You have to be careful with this though . . . all VB functions return
non-Decimal data.and assigning *that* back to the Variant that was cast as
Decimal "collapses" it back to a less robust data type. For example,
continuing the code above

X = Sqr(X)
Print X == 0.29277002188456
Jan 2 '07 #7
"Lisa" <so*****@earthlink.netwrote in message
"Jan Hyde" <St***********@REMOVE.ME.uboot.com>'s wild thoughts were
released
on Tue, 02 Jan 2007 10:41:32 GMT bearing the following
fruit:
Hi Jan
The problem is zero should not be a possibility and it is occurring
because
I have not properly converted this part of the math formula to code.
I have talked to some of the math teachers but they have not a clue as to
how it should be written in VB coding.
This is going to take someone that is knowledgably in both probability
math
and VB.
Thanks for the response
Lisa

Then the question is where are you getting the values for
'Draw' and 'MatchesReq'?
The values are come from an ACCEPTABLE range of user input.
The problem is in how I have translated the math formula into code.
>
Regardless, it's still wise to check for zero even if it
'shouldn't' happen.
If you look I did a ''blanket trap'' for errors in the first line
''On Error GoTo ErrorExit''
>
Jan Hyde (VB MVP)

--
Arch Criminal: One who robs shoe stores (Stan Kegel)

Jan 3 '07 #8

"Rick Rothstein (MVP - VB)" <ri************@NOSPAMcomcast.netwrote in
message news:zu******************************@comcast.com. ..
The problem is zero should not be a possibility and it is
occurring because I have not properly converted this part
of the math formula to code.

Are you trying to implement this formula which you posted in your first
message?

F!
------------
n!(F-n)!
where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1.

If so, you can add the BigFactorial function I show below to your project
and then just write the formula directly. That is, the above formula would
become this in code...

BigFactorial(F) / (BigFactorial(n) * BigFactorial(F - n))

Note that there is some important discussions included with the
BigFactorial
function, so you should read it carefully. While I don't say so in the
text
below, you should be aware that your calculations will become meaningless
if
the factorial is returned in exponential format due to the loss of
accuracy
inherent in having to shove large numbers into that format.

Rick

Below is a combination of two posts I given in the past to someone who
asked
about implementing a Factorial function...

You could cast (you can't Dim) a Variant variable as a Decimal type
(96-bit
number) and get some 28 or 29 digits of accuracy depending if there is a
decimal in the answer or not. Simply Dim a variable as Variant and CDec a
number (any number will do) into it to make it the Decimal type.
Thereafter,
that variable will track 28/29 digits of accuracy. For example the
following
function will calculate factorials up to 29 digits of display before
reverting to exponential display.

Function BigFactorial(ByVal N As Integer) As Variant
If N < 28 Then
BigFactorial = CDec(1)
Else
BigFactorial = CDbl(1)
End If
For x = 1 To N
BigFactorial = x * BigFactorial
Next
End Function

However, you have to watch out for overflows with Decimal data types --
once
over 28/29 characters, they will produce an overflow error. So, if you
tried
to use the above function like this

Debug.Print 10*BigFactorial(27)

you would get an overflow error but

Debug.Print 10*BigFactorial(28)

would work fine (the difference being in the first case BigFactorial has a
Decimal subtype and in the second case the subtype is a Double).

More generally, if a Variant variable is assigned a value that was cast to
Decimal, any calculation involving that variable will be "performed" as a
Decimal; and then the result cast back to the variable receiving it. If
the
result is assigned back to the variable that was originally cast to
Decimal,
that variable continues to contain a Decimal type value. For example,

X = CDec(135.6)
X = X - 135
X = X / 7
Print X == 0.0857142857142857142857142857

You have to be careful with this though . . . all VB functions return
non-Decimal data.and assigning *that* back to the Variant that was cast as
Decimal "collapses" it back to a less robust data type. For example,
continuing the code above

X = Sqr(X)
Print X == 0.29277002188456
WOW, a lot of great info and it looks like it should work.
Many thatnks and hugs
Lisa
>

Jan 3 '07 #9
"Rick Rothstein (MVP - VB)" <ri************@NOSPAMcomcast.netwrote in
message news:zu******************************@comcast.com. ..
The problem is zero should not be a possibility and it is
occurring because I have not properly converted this part
of the math formula to code.

Are you trying to implement this formula which you posted in your first
message?

F!
------------
n!(F-n)!
where F! is shorthand for (F)x(F-1)x(F-2) ...x3x2x1.

If so, you can add the BigFactorial function I show below to your project
and then just write the formula directly. That is, the above formula would
become this in code...

BigFactorial(F) / (BigFactorial(n) * BigFactorial(F - n))

Note that there is some important discussions included with the
BigFactorial
function, so you should read it carefully. While I don't say so in the
text
below, you should be aware that your calculations will become meaningless
if
the factorial is returned in exponential format due to the loss of
accuracy
inherent in having to shove large numbers into that format.

Rick
Hi Again Rick,

IT WORKED!!!!!!
As you said, I had to be careful to set the correct variant type to keep
from overflows and all went exactly as it should.

You are absolutely SUPER!!
I would have never gotten it on my own.

MANY, MANY HUGS
Lisa

Jan 5 '07 #10
IT WORKED!!!!!!
As you said, I had to be careful to set the correct variant
type to keep from overflows and all went exactly as it should.
Great! I'm glad it worked out for you.

You are absolutely SUPER!!
I would have never gotten it on my own.

MANY, MANY HUGS
Lisa
Awwww, I'm blushing. <g>
Rick
Jan 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Kennedy [UB] | last post by:
Hi Everyone, I have this multithreaded C# windows forms application which does a lot of image processing. Occasionally, I get the following error: A generic error occurred in GDI+....
6
by: Jim H | last post by:
I have an app I working on that was working fine a few days ago. Now I get a jscript error: Microsoft JScript runtime error: Object expected in a part of the code that has not been modifed or...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
1
by: Matt | last post by:
When I try to run the ASP.NET application, it has the following error messages: Error while trying to run project: unable to start debugging on the web server. The project is not configured to...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
2
by: DCC700 | last post by:
VS 2005 Converted Header causing error when publishing After converting from Visual Studio 2003 to 2005, I have had several issues with a header that is used throughout the project. The...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
4
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform...
8
by: brittany1 | last post by:
Please help i am geting a rounding up error when trying to multiply one calculated field by another. Total Time =Sum((-)*24) Value = 24.34 Calculated Rates =(*)/ Value = €10.68
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.