We help IT Professionals succeed at work.

Converting formulaes into VB routines

backflash asked

I need help trying to convert a number of formulaes into a VB Routines so I can include in a VB program.

I was trying to work out the "odds of winning various prize divisions" in the various lotto draws around the world, including Australian Lotto games. (Australian Lotto games have 2 supplementries)

I need to be able to input the following information

The number of balls in a particular Lotto Game
The number of balls drawn per game
The number of supplimentries balls drawn per game  (eg 0, 1 , 2)

so that I can find out what the odds are of getting say 5 winning numbers and 2 supps etc.

I will be using dropdown list boxes as they would take up the least room.  I want to be able to vary the information to cater for the various versions and prize divisions of the game.

I have been unable to get any books on the subject or find any web site where the explination is in a form that I can understand and use.

The only formulae I have been able to get is from the Australian Lotto Website www.tatts.com.au.

They give a brief outline on how to work them out but unfortunatly I dont have the mathmatical ability to convert them into VB Routines so I can put into my VB program.

A brief summary of the information from the Website is:  .............

A Tattslotto Draw has 45 Numbers
There are 6 winning Numbers Drawn as Well as 2 Supplimentry Numbers

Prize Division Winning Numbers Required For Tattslotto
One:  All 6 winning numbers
Two:  Any 5 winning numbers plus either supplementary
Three:  Any 5 winning numbers
Four:  Any 4 winning numbers
Five:  Any 3 winning numbers plus either supplementary

Prize Division Tattslotto Probability Formula
45c6=(45 x 44 x 43 x 42 x 41 x 40)/(1 x 2 x 3 x 4 x 5 x 6)

One 6 winning numbers 45c6
Two 5 winning numbers plus either supplementary (45c6 / (6c5 x 39c1 - 37c1))
Three 5 winning numbers (45c6 / (6c5 x 37c1))
Four 4 winning numbers (45c6 / (6c4 x 39c2))
Five 3 winning numbers plus either supplementary (45c6 / (6c3 x 39c3 - 37c3))

The explination they give for the 6 winning numbers 45c6 is easy enough to construct, but I'm unable to understand the formulae enough to get any further.

I realize  that this is no easy routine to construct, but it is beyond my ability to work it out, so I would appreciate any help at all.


Watch Question


This is a fairly complex probabilities question. How much experience do you have with probabilities?

In general, the formula will be something like:

let x be the number of primary balls drawn
let y be the number of supplimentary balls drawn
let z be the number of primary balls required
let w be the number of supplimentary balls required
let n be the number of number that you can choose

the number of ways you can choose n numbers/((the number of ways you can choose z winning balls)(the number of ways you can choose n-z-w non-winning and non supp balls)-(the number of ways you can choose w supplimentary balls)(the number of ways you can choose n-z-w non-winning and non-supp balls))

My probabilities is very rusty so the above is probably not correct, but it might help get you started.



Ive no knowledge of probility formulaes

I can understand that XXX = 45c6 is the same as XXX = (45 x 44 x 43 x 42 x 41 x 40)/(1 x 2 x 3 x 4 x 5 x 6) which is easy enough to write as a function

What I can't seen to understand is how to convert the other formuae into functions
XXX = (45c6 / (6c5 x 39c1 - 37c1))
XXX = (45c6 / (6c5 x 37c1))

I can probably nut out the programing but I need to understand the formulaes first (ie how to write them as a function.

45c6 is 45!/((45-6)!*6!)

where ! is the factorial operator which is the number times all of the integers between that number and 1.

So, I am not sure what your question is exactly, but the approach I would take to this is to create a combination function that takes in two parameters, the first number and the second number. Something like this:

Function Combination(ByVal First as long, ByVal Second as long) as long

   Dim lngAnswer as long

   lngAnswer = 1
   While First > 1
      lngAnswer = lngAnswer * First
      First = First - 1
   While Second > 1
      lngAnswer = lngAnswer / Second
      Second = Second - 1

   Combination = lngAnswer

End Function

Then, to solve (45c6 / (6c5 x 39c1 - 37c1)) , you just do


That should do it.



Sorry Its taking a while to get back to you it appears mail notification is not getting through to me.

I tried your idea on a form but kept getting an overflow error which suggests the variable type is incorrect. I,ve tried others but the same thing happens... have I missed something??


My notification is not working either. I made a mistake in the code, but the overflow happens before it gets to that point. The reason it overflows is that 45*44*43*42*41*40 is more than 2 billion, the size of a long. One way to get around this problem is to use a double, which loses some accuracy but still works. Here is the modified code:

Function Combination(ByVal First As Long, ByVal Second As Long) As Double

  Dim dblAnswer As Double
  Dim lngFirst As Long

  dblAnswer = 1
  lngFirst = First
  While lngFirst > First - Second
     dblAnswer = dblAnswer * lngFirst
     lngFirst = lngFirst - 1
  While Second > 1
     dblAnswer = dblAnswer / Second
     Second = Second - 1

  Combination = dblAnswer

End Function



Works  great on Draws without supplimentries. But I'm a bit uncertian about the accuracy when including supplimentries. Is it possible to check my reasoning

For a Lotto Draw with 2 Supplimentry numbers the Formulae is
Division3:  3 winning numbers and either supp (45c6 / (6c3 x 39c3-37c3))

Would I be right in assuming For a Lotto Draw with 1 Supplimentry numbers the Formulae is
Division3:  3 winning numbers and either supp (45c6 / (6c3 x 39c3-38c3))

These figures only allows for one supplimentry (not 2) but it does provide an avenue for checking the results from the routine

Criteria for the results below
Where there are 45 numbers in the lotto draw.
6 winning numbers are drawn and then 1 supplimentries are drawn.

6 Numbers = 8,145,060
5 Numbers = 34,808
4 Numbers = 732.8
3 Numbers = 44.6
2 Numbers = 6.6
1 Number = 2.4
0 Numbers = 2.5

5 Numbers and 1 Supplimentry = 1,357,510
4 Numbers and 1 Supplimentry = 14,290
3 Numbers and 1 Supplimentry = 579.3
2 Numbers and 1 Supplimentry = 64.4
1 Numbers and 1 Supplimentry = 18.4
0 Numbers and 1 Supplimentry = 16.2


I've been thinking about this problem, and the math is just too complex for me in my weakened state (I'm in the middle of midterm exams).



No Problems Z, Hope you do well

You've given me heaps to work with and I appreciate your help