All pastes #418962 Raw Edit

PowerBall Simulator

public python v1 · immutable
#418962 ·published 2007-03-31 23:16 UTC
rendered paste body
####  This simulates powerball drawings.##  Scroll down to the variables section before you run the simulation.##  ##  Setting playuntiljackpot to true will freeze the computer for possibly##  several hours!#### Written by BodaciousBrian##import randomclass ticket():  ## sets up the ticket object    def __init__(self):        self.red = 0        self.white = [0, 0, 0, 0, 0]        self.prizemoney = 0        self.spent = 0        self.lost = 0        self.won = 0        self.one = 0        self.two = 0        self.three = 0        self.four = 0        self.five = 0        self.six = 0        self.seven = 0        self.eight = 0        self.nine = 0def prizewon(wc, rc, pp, ticket, jp): ## this checks the ticket and compares to    if wc == 0 and rc == False:       ## the lotto numbersto determine winners        ticket.lost += 1        return    if wc == 1 and rc == False:        ticket.lost += 1        return    if wc == 2 and rc == False:        ticket.lost += 1        return        if wc == 0 and rc == True:        ticket.won += 1        ticket.one += 1        ticket.prizemoney += 3 * pp        return    if wc == 1 and rc == True:        ticket.won += 1        ticket.two += 1        ticket.prizemoney += 4 * pp        return    if wc == 2 and rc == True:        ticket.won += 1        ticket.three += 1        ticket.prizemoney += 7 * pp        return    if wc == 3 and rc == False:        ticket.won += 1        ticket.four += 1        ticket.prizemoney += 7 * pp        return    if wc == 3 and rc == True:        ticket.won += 1        ticket.five += 1        ticket.prizemoney += 100 * pp        return    if wc == 4 and rc == False:        ticket.won += 1        ticket.six += 1        ticket.prizemoney += 100 * pp        return    if wc == 4 and rc == True:        ticket.won += 1        ticket.seven += 1        ticket.prizemoney += 10000 * pp        return    if wc == 5 and rc == False:        ticket.won += 1        ticket.eight += 1        ticket.prizemoney += 200000 * pp        return    if wc == 5 and rc == True:        ticket.won += 1        ticket.nine += 1        ticket.prizemoney += jp        returndef whiteballgen(): #generates the white ball    retlist = []    while len(retlist) <= 4:         #be sure to only get 5 numbers        x = random.randint(1, 55)        if retlist.count(x) == 1:    #removes duplicates            continue        else:                        #no duplicates found, add it and continue            retlist.append(x)    return retlist                   #return the list of numbersdef redballgen():       #generates the red ball    x = random.randint(1, 42)    return xdef powerplaygen():   #i know this is weird, but this is how idaho state lotto    retint = 0        #does this picks a random number, 2, 3, 4, or 5.    x = random.randint(1, 16)    if x == 1 or x == 2 or x == 3 or x == 4:        retint = 2    if x == 5 or x == 6 or x == 7 or x == 8:        retint = 3    if x == 9 or x == 10 or x == 11 or x == 12:        retint = 4    if x == 13 or x == 14 or x == 15 or x == 16:        retint = 5    return retintdef correctwhite(ticket, lotto): ## compares ticket to lotto to see    matched = 0                  ## how many white balls match     for i in lotto:        if i in ticket:            matched += 1    return matcheddef correctred(ticket, lotto):  ##checks to see if the powerball is correct    if ticket == lotto:        return True    else:        return False####################################### variables ########################################newticket = True        ##use the same random ticket every time?powerplay = False       ##toggles powerplay functionticketprice = 1         ##price that you would pay at the store for a lotto ticketpowerplayprice = 1      ##price you pay to activate power playjackpot = 41000000      ##current jackpottimestoplay = 100000    ## number of cycles to run the simulationplayuntiljackpot = False ## run until you win(freezes for a long time)################################ program start:player = ticket()player.white = whiteballgen()player.red = redballgen()lotto = ticket()powerp = 0counter = 0if playuntiljackpot == False:    while counter < timestoplay:        counter += 1        player.spent += ticketprice        if powerplay == False:            powerp = 1        else:            powerp = powerplaygen()            player.spent += powerplayprice        if newticket == True:            player.white = whiteballgen()            player.red = redballgen()        lotto.white = whiteballgen()        lotto.red = redballgen()        wc = correctwhite(player.white, lotto.white)        rc = correctred(player.red, lotto.red)        prizewon(wc, rc, powerp, player, jackpot)if playuntiljackpot == True:    while playuntiljackpot == True:        counter += 1        player.spent += ticketprice        if powerplay == False:            powerp = 1        else:            powerp = powerplaygen()            player.spent += powerplayprice        if newticket == True:            player.white = whiteballgen()            player.red = redballgen()        lotto.white = whiteballgen()        lotto.red = redballgen()        wc = correctwhite(player.white, lotto.white)        rc = correctred(player.red, lotto.red)        prizewon(wc, rc, powerp, player, jackpot)        if player.nine == 1:            playuntiljackpot = False## show the results of the testprint "you played \t%s times" % (counter)print "you lost:    \t%s, %s percent" % (player.lost, ((1.0*player.lost / counter) * 100))print "you won something %s times %s percent" % (player.won, ((1.0*player.won / player.lost) * 100))print "chances of winning were 1 in %s" % (1.000*(player.lost / player.won))print ""print "you spent: \t%s$" % (player.spent)print "you won: \t%s$" % (player.prizemoney)print "net loss: \t%s$" % (player.spent - player.prizemoney)print "you won back %s percent of your money" % ((1.0*player.prizemoney / player.spent) * 100)print ""print "winning conditions:"print "0 white 1 red: %s" % (player.one)print "1 white 1 red: %s" % (player.two)print "2 white 1 red: %s" % (player.three)print "3 white 0 red: %s" % (player.four)print "3 white 1 red: %s" % (player.five)print "4 white 0 red: %s" % (player.six)print "4 white 1 red: %s" % (player.seven)print "5 white 0 red: %s" % (player.eight)print "5 white 1 red: %s" % (player.nine)