Skip to content

Instantly share code, notes, and snippets.

Created February 4, 2013 23:56
Show Gist options
  • Save anonymous/4710891 to your computer and use it in GitHub Desktop.
Save anonymous/4710891 to your computer and use it in GitHub Desktop.
# Lotto simulator ...
# here we let the user pick the winning numbers and the computer buys the tickets
#
# the user selects 6 unique random numbers from 1 to 50 and creates a list
# the computer generates similar lists and compares these lists with the user list
# whenever there are 3, 4, 5 or 6 matches, corresponding counters are updated
# finally all the matches are shown
# tested with Python24 vegaseat 14sep2006
import random
from collections import defaultdict, Counter
# The computer picks the numbers for each ticket sold
balls = 4
higher_ball = 100
tickets_sold = 10000000
cost_per_play = 0.1
def computer_random():
return random.sample(range(1, higher_ball), balls)
def match_lists(list1, list2):
# to find the number of matching items in each list use sets
set1 = set(list1)
set2 = set(list2)
# set3 contains all items comon to set1 and set2
set3 = set1.intersection(set2)
# return number of matching items
return len(set3)
# Picks the winning numbers
user_list = computer_random()
# Init Counter
matches_counts = defaultdict(int)
refund = 0
bigprize = 0
smallprize = 0
microprize = 0
fee = 0
print "\n# -- Lottery Statistics"
print "\nWinning numbers:", user_list
print "\nRandomizing %i Tickets from 1 to %i ..." % (tickets_sold, higher_ball)
for k in range(tickets_sold):
# Random Generator
comp_list = computer_random()
matches = match_lists(comp_list, user_list)
# Big Prize
if matches == 1:
refund += cost_per_play
else:
bigprize += cost_per_play * 0.20
smallprize += cost_per_play * 0.30
microprize += cost_per_play * 0.40
# Fee
fee += cost_per_play * 0.10
# Increase Matches Counter
matches_counts[matches] += 1
print "\nOut of %d tickets sold the computer found these matches:\n" % tickets_sold
for count in range(balls + 1):
print " - Matching %i Numbers: %i" % (count, matches_counts[count])
print ''
print ' [1] - Refunded: %.08f BTCs' % refund
if matches_counts[2] == 0:
print ' [2] - Micro Prize: %.08f BTCs : No winners :(' % microprize
else:
print ' [2] - Micro Prize: %.08f BTCs : %.8f BTCs per winner' % (microprize, microprize / matches_counts[2])
if matches_counts[3] == 0:
print ' [3] - Small Prize: %.08f BTCs : No winners :(' % (bigprize)
else:
print ' [3] - Small Prize: %.08f BTCs : %.8f BTCs per winner' % (smallprize, smallprize / matches_counts[3])
if matches_counts[4] == 0:
print ' [4] - Big Prize: %.08f BTCs : No winners :(' % (bigprize)
else:
print ' [4] - Big Prize: %.08f BTCs : %.8f BTCs per winner' % (bigprize, bigprize / matches_counts[4])
print "\n House Fees: %.08f" % fee
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment