Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal of this PR is to implement a function that takes a natural number
n
and parameterk
, and uniformly splitsn
intok
numbersn1..nk
that sum back ton
(n1 + ... +nk = n
). This is useful when splitting "fuel" or "size" in a random generator, amongk
components instead of just two. (cc @olivier-martinot, with whom I am collaborating on a random generator that would benefit.)Two variants are provided: in
nat_split
, then1..nk
are natural numbers (range[0;n]
), inpos_split
they are strictly positive (range[1;n]
, or in fact[1;n-k]
).The code for these functions is simple and relies on a generic helper that is also useful. However getting them right is tricky, and random generators are hard to test. I checked the implementation in an experimental version of my random-generator library that computes finite distributions.
Apparently there are
21
different ways to split5
into three natural numbers, and6
ways to split it into three strictly-positive numbers. (The pair(a, b)
before an output represents the rational numbera/b
, the probability of observing this output.)