Created
June 18, 2014 15:01
-
-
Save christianp/39d8b5b4cc7bdcb1a69e to your computer and use it in GitHub Desktop.
If sticker collectors pool their acquisitions but collectors leave the pool as soon as they're finished, how much does each collector spend?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Album has 638 stickers | |
50 trials for each pool size | |
Collectors Waste Waste per collector Mean Min bought Mean Max bought | |
1 3921.400000 3921.400000 4559.400000 4559.400000 | |
2 4464.800000 2232.400000 2463.120000 3277.680000 | |
3 5422.080000 1807.360000 2009.160000 2993.760000 | |
4 5517.640000 1379.410000 1592.040000 2582.040000 | |
5 6359.600000 1271.920000 1459.800000 2700.000000 | |
6 6726.600000 1121.100000 1318.200000 2594.760000 | |
7 7176.520000 1025.217143 1213.560000 2506.800000 | |
8 7732.520000 966.565000 1178.280000 2606.640000 | |
9 7711.920000 856.880000 1081.800000 2442.120000 | |
10 8506.360000 850.636000 1069.080000 2612.160000 | |
11 8837.120000 803.374545 1028.160000 2554.440000 | |
12 9001.800000 750.150000 1002.600000 2488.200000 | |
13 9497.320000 730.563077 979.080000 2531.280000 | |
14 9190.520000 656.465714 937.800000 2448.360000 | |
15 9705.960000 647.064000 924.480000 2365.920000 | |
16 10108.120000 631.757500 901.080000 2490.360000 | |
17 9856.760000 579.809412 881.280000 2285.400000 | |
18 10193.040000 566.280000 867.000000 2306.520000 | |
19 10359.400000 545.231579 849.240000 2324.400000 | |
20 10716.080000 535.804000 841.920000 2380.800000 | |
Waste is the number of excess stickers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Collection: | |
def __init__(self,num_stickers): | |
self.num_stickers = num_stickers | |
def pack(self): | |
return [random.randrange(self.num_stickers) for i in range(6)] | |
class Album: | |
bought = 0 | |
def __init__(self,collection): | |
self.collection = collection | |
self.got = [False]*self.collection.num_stickers | |
def finished(self): | |
return False not in self.got | |
def collect(collection,num_albums): | |
albums = [Album(collection) for i in range(num_albums)] | |
doubles = [] | |
unfinished = albums[:] | |
done = False | |
while sum(album.finished() for album in albums)<num_albums: | |
buyer = random.choice(unfinished) | |
buyer.bought += 6 | |
pack = collection.pack() | |
for sticker in pack: | |
double = True | |
order = albums[:] | |
random.shuffle(order) | |
for album in order: | |
if not album.got[sticker]: | |
album.got[sticker] = True | |
double = False | |
if album.finished(): | |
unfinished.remove(album) | |
break | |
if double: | |
doubles.append(sticker) | |
bought = [album.bought for album in albums] | |
return (len(doubles),min(bought),max(bought)) | |
if __name__ == '__main__': | |
num_stickers = 638 | |
num_trials = 50 | |
collection = Collection(num_stickers) | |
print("Album has %i stickers" % num_stickers) | |
print("%i trials for each pool size" % num_trials) | |
print('Collectors\tWaste\t\tWaste per collector\tMean Min bought\tMean Max bought') | |
for i in range(1,21): | |
t_doubles = 0 | |
t_min_bought = 0 | |
t_max_bought = 0 | |
for j in range(num_trials): | |
doubles, min_bought, max_bought = collect(collection,i) | |
t_doubles += doubles | |
t_min_bought += min_bought | |
t_max_bought += max_bought | |
t_doubles /= num_trials | |
t_min_bought /= num_trials | |
t_max_bought /= num_trials | |
print('%i\t\t%f\t%f\t\t%f\t%f' % (i,t_doubles,t_doubles/i,t_min_bought,t_max_bought)) | |
print("Waste is the number of excess stickers") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment