{{This article was originally posted on December 22, 2016. My posts were lost and after being found I am re-posting them.}}
One day (in 2011) got a whim to try and write a Python program to that will try and find the best combinations of numbers (within given range and given tolerances) that will add up to a fixed target total. Didn’t think of much use for it then, but have used it in multiple instances to get some good results. The algorithm works as the diagram below :

The Python code for it :
import random def generateBucketList(): fixedTargetTotal = 94 bucketMidPoints = [[10, 5], [5, 10], [6], [5], [5], [6], [6], [8], [6], [8], [5, 5, 5, 5]] bucketAssign = [1, 2, 3] rangeFromMidPoint = [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4] bucketMidPoints = sum(bucketMidPoints, []) finalAssign = [] if sum(bucketMidPoints) > fixedTargetTotal: # reduce(lambda x, y: x + y, bucketMidPoints) > fixedTargetTotal: while sum(bucketAssign) != fixedTargetTotal: # reduce(lambda x, y: x + y, bucketAssign) != fixedTargetTotal: # bucketAssign = [random.randint(int(0.6*i),i) for i in bucketMidPoints] bucketAssign = [] for i in bucketMidPoints: if i != 0: bucketAssign.append(random.randint(int(0.6 * i), i)) else: bucketAssign.append(i) else: marks = "Bucket dispersal is not above given expected Target Total" # Print the list in given order from here startMarker = 0 endMarker = 0 for i in rangeFromMidPoint: endMarker = endMarker + i tempList = bucketAssign[startMarker: endMarker] finalAssign.append(tempList) startMarker = startMarker + i for i, j in enumerate(finalAssign): print i + 1, " -> ", j print "\n" print bucketAssign, sum(bucketAssign) # reduce(lambda x, y: x + y, bucketAssign) if __name__ == "__main__": generateBucketList() print "Demo!!!..."