Skip to content

Instantly share code, notes, and snippets.

@hschmitt
Created January 25, 2013 18:19
Show Gist options
  • Save hschmitt/4636641 to your computer and use it in GitHub Desktop.
Save hschmitt/4636641 to your computer and use it in GitHub Desktop.
from random import random, randint
from math import pow, sqrt
car_probs = {
0.3: (5, 20),
0.8: (21, 40),
1.0: (41, 60),
}
day_hours = 24
def galon_prob():
return randint(1,7)
def cars_per_hour():
throw = random()
for prob in car_probs:
if throw <= prob:
return randint(*car_probs[prob])
def simulation():
total_cars = 0
total_galons = 0
for car in range(24):
cars_this_hour = cars_per_hour()
total_cars += cars_this_hour
for vehicle in range(cars_this_hour):
total_galons += galon_prob()
print "Total cars this day: " + str(total_cars)
print "Total galons sold this day: " + str(total_galons)
print "Total price of gas: S/." + str(total_galons * 15)
if __name__ == "__main__":
simulation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment