-, , . " 15 60 , 100", " n 0 45 (60 - 15), 100 - nx 15". , 15 , , , .
, n , p . r 0 45, p-r. (n-1) ?
- (p - r) < 0, , , , , ,
- (p - r) > 45 * (n - 1), : , .
- , r , .
: , .
ββ F #, , , /, . - int, , , qty - , , , . , 100 , 100 3 15 60.
The resulting distribution should be "as random as it turns out", that is, it should give with equal probability any possible distribution with one caveat: I suggested that the order of the buckets does not matter. Given that the algorithm allocates the remaining potatoes based on what has been allocated so far, depending on the path, the "head" of the list is limited, and the distribution is likely to allocate more to the first buckets. If you want to have a βtrueβ random distribution, you will need to shuffle the buckets after the selection is complete.
Hope this helps!
let rng = new System.Random()
let rec allocate allocation qty buckets min max =
match buckets with
| 0 -> allocation
| 1 -> qty :: allocation
| _ ->
let candidate = rng.Next(min, max + 1) // try a number in [min,max]
let rest = qty - candidate
if rest < (buckets - 1) * min // not enough left
then allocate allocation qty buckets min max // try again
else if rest > (buckets - 1) * max // too much left
then allocate allocation qty buckets min max // try again
else allocate (candidate :: allocation) rest (buckets - 1) min max
let solve qty buckets min max =
if min * buckets > qty then failwith "unfeasible"
if max * buckets < qty then failwith "unfeasible"
allocate [] qty buckets min max