File 1, number selection program, xh. py
The code is as follows: |
Copy code |
# File 1 xh. py Import random
Def getResultStr (totalCount, resultCount ): Elements = [x + 1 for x in range (totalCount)] RetStr ='' For I in range (resultCount ): Res = elements [random. randint (0, len (elements)-1)] Elements. remove (res) RetStr + = ''+ str (res)
Return retStr |
The code is as follows: |
Copy code |
File 2, ssq. py
# File 2 ssq. py Import xh
Print xh. getResultStr (33, 6) Print xh. getResultStr (16, 1) |
File 3, Lotto dlt. py
The code is as follows: |
Copy code |
# File 3 dlt. py Import xh
Print xh. getResultStr (33, 6) Print xh. getResultStr (16, 1) |
Python-based simulation of the lottery purchase-to-lottery process
Import random import time # Chinese processing function def ch (words ):
Return words. decode ('utf-8 ')
# Sort the generated sequence of lottery numbers from large to small. def paixu (array ):
Templist = list (array) count = len (templist) for I in range (0, count-1): for j in range (0, count-1 ): if templist <templist [j]: temp = templist [j] templist [j] = temp return templist
# Generate the lottery function. There may be repeated numbers def shengchengcaipiao ():
Jixuancaipiao = [0, 0, 0, 0, 0]
For xunhuan1 in range (0, 6 ):
Jixuancaipiao [xunhuan1] = random. randint () # Change here to control the number range: jixuancaipiao [6] = random. randint () return jixuancaipiao # Check repeated functions
Def jiancechongfu (weipaixucaipiao ):
Tempweipaixucaipiao = list (weipaixucaipiao) for xunhuan2 in range (0, 5 ):
For xunhuan3 in range (xunhuan2 + 1, 6 ):
If tempweipaixucaipiao [xunhuan2] = tempweipaixucaipiao [xunhuan3]: return False return True # Generate the lottery function def ticketOK ():
Temp = list (shengchengcaipiao () if jiancechongfu (temp): return paixu (temp) else:
Return ticketOK () # recursion
Print ch ('Welcome to the two-color ball simulated purchase process') # purchase quantity
QTYbuy = input (ch ('purchase quantity: ') time1 = time. clock ()
# Initialize the winning status Statistics array
# 'CS 'stand for 'correctstatus'
Print ch ('Start initializing the winning status Statistics array') CS = [0 for I in range (QTYbuy)]
Print ch ('initialization of the winning status Statistics array complete') # lottery number
CorrectTicket = ticketOK () # Initialize the array of purchased lottery tickets
Print ch ('initialize the purchased lottery array ')
TicketBought = [[0 for I in range (7)] for j in range (QTYbuy)] print ch ('initialization of purchased lottery array complete ') # Initialize the lottery ticket number grade1 = 0 grade2 = 0 grade3 = 0 grade4 = 0 grade5 = 0
Grade6 = 0 # purchase lottery tickets
Print ch ('purchasing lottery tickets ')
For ticketcount in range (QTYbuy): temp1 = list (ticketOK () for ticketnum in range (7 ):
TicketBought [ticketcount] [ticketnum] = temp1 [ticketnum] print ch ('purchase lottery Times'), time. clock ()-time1, ch ('second') # count the winning lottery tickets
Print ch ('counting the winning lotteries ') for ticketcount1 in range (0, QTYbuy): red = 0 blue = False
For ticketnum in range (6 ):
If TicketBought [ticketcount1] [6] = CorrectTicket [6]: blue = True
For correctTicketnum in range (6 ):
If TicketBought [ticketcount1] [ticketnum] = CorrectTicket [correctTicketnum]: red = red + 1 if red = 6 and blue: # First Prize CS [ticketcount1] = 1 elif red = 6: # Second Prize CS [ticketcount1] = 2
Elif red = 5 and blue: # Third Prize
CS [ticketcount1] = 3
Elif red = 5 or (red = 4 and blue): # class 4 award CS [ticketcount1] = 4
Elif red = 4 or (red = 3 and blue): # fifth prize CS [ticketcount1] = 5 elif blue: # sixth prize CS [ticketcount1] = 6 # print ch ('Statistics on the number of winning scores of different levels ') for ticketcount2 in range (QTYbuy ): if CS [ticketcount2] = 1: grade1 = grade1 + 1 if CS [ticketcount2] = 2: grade2 = grade2 + 1 if CS [ticketcount2] = 3: grade3 = grade3 + 1 if CS [ticketcount2] = 4: grade4 = grade4 + 1 if CS [ticketcount2] = 5: grade5 = grade5 + 1
If CS [ticketcount2] = 6: grade6 = grade6 + 1 # winning amount statistics
Prize = grade1 * 5000000 + grade2 * 800000 + grade3 * 3000 + grade4 * 200 + grade5 * 10 + grade6 * 5 # opening ceremony
Print ch ('Current prize number: '), CorrectTicket # for I in range (QTYbuy ):
# Print ch ('die'), I + 1, ch ('Note: '), # for j in range (7 ):
# Print TicketBought [j], # if CS:
# Print ch (''), CS, ch ('') # else:
# Print ch ('no winning ')
# If you cancel the above comments, the "lottery" process can be displayed in real time, but the running speed will be slowed down.
If grade1 or grade2 or grade3 or grade4 or grade5 or grade6: print ch ('This purchase, you are in the middle ') else:
Print ch ('Sorry, this purchase did not win. ') if grade1:
Print grade1, ch ('Note 1, etc. ') if grade2:
Print grade2, ch ('Note 2, etc ')
If grade3:
Print grade3, ch ('Note 3, etc. ') if grade4:
Print grade4, ch ('Note 4, etc. ') if grade5:
Print grade5, ch ('Note 5, etc. ') if grade6:
Print grade6, ch ('Note 6, etc ')
Print ch ('Winning amount: '), prize, ch ('Yuan'), print ch ('total invested '), QTYbuy * 2, ch ('Yuan RMB ')
Print ch ('yield: '), 50.000 * (prize-QTYbuy * 2)/QTYbuy,' % 'print ch ('Operation time'), time. clock ()-time1, ch ('second ')