ElKamina 的答案可能是正确的,但对于数学上的挑战,这里是另一种马铃薯分类算法。
from random import randint, choice
def random_distribution(num_potatoes, num_buckets, min_num_potatoes, max_num_potatoes):
"""
Distributes a set number of potatoes randomly across a number of buckets
such that each bucket has a number of potatoes within a certain range.
"""
# Cache the starting value
_num_potatoes = num_potatoes
# Create some buckets to put potatoes in, initial value 0
buckets = [0 for x in range(num_buckets)]
# Get a list of which buckets are not too empty or too full--
# All of them are valid choices at this point
indices = [i for (i, value) in enumerate(buckets)]
# Distribution loop-- continue until we run out of potatoes or the
# buckets are too full to keep adding to them.
while indices and num_potatoes and sum(buckets) <= (max_num_potatoes * num_buckets):
# Pick a random bucket from the list of eligible buckets
index = choice(indices)
# Add a potato to it
buckets[index] = buckets[index] + 1
num_potatoes = num_potatoes - 1
# Refresh the list of eligible buckets
indices = [i for (i, value) in enumerate(buckets) if (value < min_num_potatoes) or (value < max_num_potatoes)]
print('Of %(potatoes)i potatoes distributed across %(buckets)i buckets, each having at least %(min)i and at most %(max)i, our spread looks like %(spread)s (total bucketed: %(sum)i).' % {
'potatoes': _num_potatoes,
'buckets': num_buckets,
'min': min_num_potatoes,
'max': max_num_potatoes,
'spread': buckets,
'sum': sum(buckets)
})
return buckets
for x in range(10):
random_distribution(100, 3, 15, 60)
示例输出:
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [35, 37, 28] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [29, 36, 35] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [32, 27, 41] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [33, 33, 34] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [32, 32, 36] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [37, 28, 35] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [32, 27, 41] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [39, 27, 34] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [32, 30, 38] (total bucketed: 100).
Of 100 potatoes distributed across 3 buckets, each having at least 15 and at most 60, our spread looks like [32, 30, 38] (total bucketed: 100).
在您给定的示例中,如果您想要更多的价值差异,您需要从更少的土豆开始或使用更多的桶。