Announcement

Collapse
No announcement yet.

La Lottery Statistics

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • La Lottery Statistics

    In an effort to mess around with Objects as well as calculate some statistics from the Louisiana Lottery, I converted a program I wrote to generate every possible Lottery number possible and then calculate how many prizes you would win and how much money you would win if you could buy every single combination of lottery tickets available. (just for the heck of it of course) So I modified it a bit in the process in an effort to make it more effiecient. I changed it to count backwards as that usually buys you a few extra cycles per loop. I also changed the main variables to use bit-type, but that was for a whole different reason that didn't work out.... Anyway, here's the code for Object Oriented folks to study....

    Code:
    #COMPILE EXE
    #DIM ALL
    TYPE LaLottoType
           n1 AS BIT * 6 IN DWORD
           n2 AS BIT * 6
           n3 AS BIT * 6
           n4 AS BIT * 6
           n5 AS BIT * 6
           n6 AS BIT * 6 IN WORD
    END TYPE
    UNION uLaLotto
        i AS LaLottoType
        d AS QUAD
    END UNION
    CLASS TLaLottery
        INSTANCE num AS uLaLotto
        INSTANCE tik() AS uLaLotto
        INSTANCE Valid AS DWORD
     
        INTERFACE iLaLottery
          INHERIT IUNKNOWN
          METHOD SortNumbers()
          DIM z(1 TO 6) AS BYTE
          DIM t AS INTEGER
     
                ARRAY ASSIGN z() = num.i.n1,num.i.n2,num.i.n3,num.i.n4,num.i.n5,num.i.n6
                ARRAY SORT z()
                num.i.n1 = z(1)
                num.i.n2 = z(2)
                num.i.n3 = z(3)
                num.i.n4 = z(4)
                num.i.n5 = z(5)
                num.i.n6 = z(6)
          END METHOD
     
          METHODnts,eticket() AS STRING
          DIM s AS STRING
               'me.SortNumbers()
               s = USING$("##",num.i.n1) + "," + USING$("##",num.i.n2) + "," + USING$("##",num.i.n3) + ","
               s = s + USING$("##",num.i.n4) + "," + USING$("##",num.i.n5) + "," + USING$("##",num.i.n6)
               METHOD = s
           END METHOD
           METHOD IsValidLottery() AS BYTE
           DIM tik(1 TO 10000000) AS uLaLotto
           DIM ex AS BYTE
           ex = -1
           me.SortNumbers()
           IF (num.i.N1 < 1) OR (num.i.N2 < 1) OR (num.i.N3 < 1) OR (num.i.N4 < 1) OR (num.i.N5 < 1) OR (num.i.N6 < 1) THEN ex = 0
           IF (num.i.N1 > 40) OR (num.i.N2 > 40) OR (num.i.N3 > 40) OR (num.i.N4 > 40) OR (num.i.N5 > 40) OR (num.i.N6 > 40) THEN ex = 0
           IF (num.i.N1 = num.i.N2) OR (num.i.N1 = num.i.N3) OR (num.i.N1 = num.i.N4) OR (num.i.N1 = num.i.N5) OR (num.i.N1 = num.i.N6) THEN ex = 0
           IF (num.i.N2 = num.i.N3) OR (num.i.N2 = num.i.N4) OR (num.i.N2 = num.i.N5) OR (num.i.N2 = num.i.N6) THEN ex = 0
           IF (num.i.N3 = num.i.N4) OR (num.i.N3 = num.i.N5) OR (num.i.N3 = num.i.N6) THEN ex = 0
           IF (num.i.N4 = num.i.N5) OR (num.i.N4 = num.i.N6) THEN ex = 0
           IF (num.i.N5 = num.i.N6) THEN ex = 0
           METHOD = ex
           END METHOD
           METHOD NumbersMatch(m1 AS BYTE, m2 AS BYTE, m3 AS BYTE, m4 AS BYTE, m5 AS BYTE, m6 AS BYTE) AS INTEGER
           DIM a1 AS BYTE
           DIM a2 AS BYTE
           DIM a3 AS BYTE
           DIM a4 AS BYTE
           DIM a5 AS BYTE
           DIM a6 AS BYTE
           DIM q(1 TO 6) AS BYTE
           DIM c AS INTEGER
           DIM rs AS INTEGER
               ARRAY ASSIGN q() = m1,m2,m3,m4,m5,m6
               ARRAY SORT q()
     
               rs = 0
               FOR c = 1 TO 6
                   IF (num.i.n1 = q(c)) THEN rs = rs + 1
                   IF (num.i.n2 = q(c)) THEN rs = rs + 1
                   IF (num.i.n3 = q(c)) THEN rs = rs + 1
                   IF (num.i.n4 = q(c)) THEN rs = rs + 1
                   IF (num.i.n5 = q(c)) THEN rs = rs + 1
                   IF (num.i.n6 = q(c)) THEN rs = rs + 1
               NEXT c
           METHOD = rs
           END METHOD
     
           METHOD AllLotto
           DIM Tik(1 TO 10000000) AS uLaLotto
           DIM z AS QUAD
           DIM q AS QUAD
           DIM p AS uLaLotto
           DIM ex AS INTEGER
           DIM Valid AS QUAD
           DIM ticket AS QUAD
           DIM Winnings AS CURRENCY
           DIM w6 AS QUAD
           DIM w5 AS QUAD
           DIM w4 AS QUAD
           DIM w3 AS QUAD
           DIM a AS INTEGER
           DIM b AS INTEGER
           DIM c AS INTEGER
           DIM d AS INTEGER
           DIM e AS INTEGER
           DIM f AS INTEGER
           DIM winners AS QUAD
     
               Valid = 0
               winners = 0
               winnings = 0.00
               w3 = 0
               w4 = 0
               w5 = 0
               w6 = 0
               CONSOLE SET SCREEN 25,80
               PAGE 1,1
               CLS
               FOR a = 40 TO 1 STEP -1
                   FOR b = 39 TO 1 STEP -1
                       FOR c = 38 TO 1 STEP -1
                           FOR d = 37 TO 1 STEP -1
                               FOR e = 36 TO 1 STEP -1
                                   FOR f = 35 TO 1 STEP -1
                                       num.i.n1 = a
                                       num.i.n2 = b
                                       num.i.n3 = c
                                       num.i.n4 = d
                                       num.i.n5 = e
                                       num.i.n6 = f
                                       me.SortNumbers()
                                       IF me.IsValidLottery() THEN
                                         PAGE 2,1
                                         CLS
                                         ex = 0
                                         INCR ticket
                                         IF (Valid < 1) THEN
                                           Tik(1) = num
                                           INCR Valid
                                         ELSE
                                           FOR q = 1 TO Valid
                                             IF Tik(q).d = num.d THEN ex = -1
                                           NEXT q
                                         IF ex = 0 THEN
                                           INCR Valid
                                           Tik(Valid) = num
                                           LOCATE 3,1
                                           COLOR 15
                                           PRINT "Ticket # " + USING$("###,###,###,###",ticket) + " : ";
                                           COLOR 11
                                           PRINT me.MakeTicket() + "  "
                                           IF me.NumbersMatch(1,2,3,4,5,6) = 6 THEN
                                             COLOR 10
                                             PRINT "JackPot!"
                                             Winnings = Winnings + 100000.00
                                             INCR Winners
                                             INCR w6
                                           END IF
                                         IF me.NumbersMatch(1,2,3,4,5,6) = 5 THEN
                                           COLOR 14
                                           PRINT "$2,000.00"
                                           Winnings = Winnings + 2000.00
                                           INCR Winners
                                           INCR w5
                                         END IF
                                         IF me.NumbersMatch(1,2,3,4,5,6) = 4 THEN
                                           COLOR 9
                                           PRINT "$50"
                                           Winnings = Winnings + 50.00
                                           INCR Winners
                                           INCR w4
                                         END IF
                                         IF me.NumbersMatch(1,2,3,4,5,6) = 3 THEN
                                           COLOR 12
                                           PRINT "$3"
                                           Winnings = Winnings + 3.00
                                           INCR Winners
                                           INCR w3
                                         END IF
                                         IF me.NumbersMatch(1,2,3,4,5,6) < 3 THEN
                                           COLOR 13
                                           PRINT "Looser!"
                                         END IF
                                         PRINT
                                         COLOR 10,0
                                         PRINT "Winners : " + USING$("###,###,###,###",winners)
                                         PRINT "Winnings: " + USING$("###,###,###,###.##",winnings)
                                         PRINT "Match 3 : " + USING$("###,###,###,###",w3)
                                         PRINT "Match 4 : " + USING$("###,###,###,###",w4)
                                         PRINT "Match 5 : " + USING$("###,###,###,###",w5)
                                         PRINT "Match 6 : " + USING$("###,###,###,###",w6)
                                         PAGE 1,1
                                         PCOPY 2,1
                                       END IF
                                     END IF
                                   END IF
                                   PAGE 1,1
                                   PCOPY 2,1
                                 NEXT f
                               NEXT e
                           NEXT d
                       NEXT c
                   NEXT b
               NEXT a
           END METHOD
     
        END INTERFACE
    END CLASS
    FUNCTION PBMAIN () AS LONG
    DIM l AS iLaLottery
     &nle o  LET l = CLASS "tLaLottery"
     
        CLS
        l.AllLotto()
     
    '    OPEN "Lottery.csv" FOR OUTPUT AS #1
    '    PRINT #1, "n1,n2,n3,n4,n5,n6,matches,winnings"
        PRINT "Press any key to continue."
        WAITKEY$
    END FUNCTION
    Attached Files

  • #2
    The following has nothing to do with Object Oriented approach but greatly increases performance

    IsValidLottery method

    First remove DIM tik(1 TO 10000000) AS uLaLotto as this isn't used. (Biggest gain)


    Since the lottery numbers are being sorted. The following if statement can be simplified...

    N1 < 1 then no need to check other numbers as N1 is the smallest number
    N6 > 40 then no need to check other numbers as they will be all smaller than N6

    Since numbers are sorted checks only need to be N1 = n2, n2 = n3, n3=n4, etc... i.e. if N1 = N3 then N1 must also = N2.

    Once an not valid is reached there no reason to continue checking the others, so I converted to IF ElseIf format. Each if could return Method =0 and exit function, but if you want a single exit point... Just a matter of style.

    The IF statements may be able to optimized into a different order for better performance.


    Code:
           METHOD IsValidLottery() AS BYTE
    
               'DIM tik(1 TO 10000000) AS uLaLotto
               DIM ex AS BYTE
               ex = -1
               me.SortNumbers()
               IF (num.i.N1 < 1) THEN
                   ex = 0
               ELSEIF (num.i.N6 > 40) THEN
                   ex = 0
               ELSEIF (num.i.N1 = num.i.N2) THEN
                   ex = 0
               ELSEIF (num.i.N2 = num.i.N3) THEN
                   ex = 0
               ELSEIF (num.i.N3 = num.i.N4) THEN
                   ex = 0
               ELSEIF (num.i.N4 = num.i.N5) THEN
                   ex = 0
               ELSEIF (num.i.N5 = num.i.N6) THEN
                   ex = 0
               END IF
    
               METHOD = ex
    
           END METHOD

    Comment


    • #3
      Wow, what a difference!

      I made the changes you suggested, and changed the for/next loops to count backward as that tends to be faster on most languages.... The net result is the program now flys! I've gotten way farther in just a matter of hours than I got from leaving the computer on all night long before! I really wasn't so much into optimizing the program as I was just looking for a working solution to guarantee me that I generated every single possible ticket.... I'm thinking of registering the Console Tools Library and the graphics library together, when my tax refund comes in... If I go that route, I'll see about adding a few extras, including a clock, stats bar, and some kinda of way of letting the user know what's going on when it's rejecting duplicates or invalid numbers..... I'd like something a little more than just displaying "invalid" .... Once I get this version the way I like it, I'll just make a few modifications (or make another object that inherits the basic functions and modifies the main loop) to calculate all possible powerball tickets.....

      Comment

      Working...
      X