Skip to content

Instantly share code, notes, and snippets.

@leb2
Last active October 21, 2015 02:30
Show Gist options
  • Save leb2/415aefcc53f84a2d1006 to your computer and use it in GitHub Desktop.
Save leb2/415aefcc53f84a2d1006 to your computer and use it in GitHub Desktop.
import random
time_chances = [
(0, 0.10),
(1, 0.15),
(2, 0.10),
(3, 0.35),
(4, 0.25),
(5, 0.05)
]
service_chances = [
# (1, 0.3000),
# (2, 0.2500),
# (3, 0.45)
(1, .25),
(2, .20),
(3, .40),
(4, .15)
]
def weighted_choice(choices):
total = sum(w for c, w in choices)
r = random.uniform(0, total)
upto = 0
for c, w in choices:
if upto + w > r:
return c
upto += w
if __name__ == "__main__":
averages = []
line_averages = []
line_per_min = []
for j in range(10000):
people = []
for i in range(150):
service_time = weighted_choice(service_chances)
time_til_next = weighted_choice(time_chances)
people.append({
'service_time': service_time,
'time_til_next': time_til_next,
'wait_time': 0,
'line_length': 0,
'id': i
})
people_done = []
line = [people.pop()]
service_left = line[0]['service_time']
time_left = line[0]['time_til_next']
while len(line) > 0 or len(people) > 0:
line_per_min.append(len(line))
for person in line:
if not person['id'] == line[0]['id']:
person['wait_time'] += 1
time_left -= 1
service_left -= 1
# If done remove first person from line
if service_left <= 0 and service_left > -1:
people_done.append(line.pop(0))
# People walk in
while time_left <= 0:
if len(people) > 0:
new_person = people.pop() # Get next person from pool
new_person['line_length'] = len(line)
line.append(new_person) # Add person to start of line
time_left = new_person['time_til_next']
else: break
if len(line) > 0 and service_left <= 0:
service_left = line[0]['service_time']
wait_times = [person['wait_time'] for person in people_done]
line_lengths = [person['line_length'] for person in people_done]
averages.append(sum(wait_times) / float(len(people_done)))
line_averages.append(sum(line_lengths) / float(len(people_done)))
print "\nAverage Line (minute): " + str(sum(line_per_min) / float(len(line_per_min)))
print "Average Line (person): " + str(sum(line_averages) / len(line_averages))
print "Average Wait: " + str(sum(averages) / len(averages)) + "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment