33
4
Your task is to write a program or a function that outputs n
random numbers from interval [0,1] with fixed sum s
.
Input
n, n≥1
, number of random numbers to generate
s, s>=0, s<=n
, sum of numbers to be generated
Output
A random n
-tuple of floating point numbers with all elements from the interval [0,1] and sum of all elements equal to s
, output in any convenient unambiguous way. All valid n
-tuples have to be equally likely within the limitations of floating point numbers.
This is equal to uniformly sampling from the intersection of the points inside the n
-dimensional unit cube and the n-1
-dimensional hyperplane that goes through (s/n, s/n, …, s/n)
and is perpendicular to the vector (1, 1, …, 1)
(see red area in Figure 1 for three examples).
Figure 1: The plane of valid outputs with n=3 and sums 0.75, 1.75 and 2.75
Examples
n=1, s=0.8 → [0.8]
n=3, s=3.0 → [1.0, 1.0, 1.0]
n=2, s=0.0 → [0.0, 0.0]
n=4, s=2.0 → [0.2509075946818119, 0.14887693388076845, 0.9449661625992032, 0.6552493088382167]
n=10, s=9.999999999999 → [0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999,0.9999999999999]
Rules
- Your program should finish under a second on your machine at least with
n≤10
and any valid s. - If you so wish, your program can be exclusive on the upper end, i.e.
s<n
and the output numbers from the half-open interval [0,1) (breaking the second example) - If your language doesn't support floating point numbers, you can fake the output with at least ten decimal digits after the decimal point.
- Standard loopholes are disallowed and standard input/output methods are allowed.
- This is code-golf, so the shortest entry, measured in bytes, wins.
Related. – Martin Ender – 2018-05-18T12:15:48.580
When you say
This is equal to uniformly sampling from the intersection
- i can see a program choosing randomly from just the corners of that intersection. Would that be valid ? – JayCe – 2018-05-18T12:43:12.120Because not all languages have floating point support, (at least) how many digit of accuracy/precision must be supported? (the suggested test case above implies 13, but you may want a softer restriction) – user202729 – 2018-05-18T12:47:24.610
2@KevinCruijssen No, that's only true for
s==0 or s==3
. For all other values ofs
, the plane has nonzero area and you have to uniform-randomly choose a point on that plane. – user202729 – 2018-05-18T13:05:49.133@JayCe that would be invalid, since all valid n-tuples have to be equally likely. – Angs – 2018-05-18T13:08:24.740
One way to do this is to use this approach mirrored at the centre and then using rejection sampling
– Angs – 2018-05-18T14:07:39.943"Integer"? But the question ask for floating point output. – user202729 – 2018-05-18T14:15:31.037
All valid n-tuples have to be equally likely within the limitations of floating point numbers.
do you mean same probably for same size? (usually such definition, but not always) – l4m2 – 2018-05-18T16:08:11.023@l4m2 when the parameters
n
ands
are fixed, the valid outputs should be about equally likely. The order matters, so for instance(0.2, 0.3, 0.1)
and(0.3, 0.2, 0.1)
both should be as common as(0.6, 0.0, 0.0)
forn=3, s=0.6
– Angs – 2018-05-18T16:37:15.677@Angs so any distribution is allowed as the possibility are all zero? – l4m2 – 2018-05-18T16:47:34.287
@l4m2, no, equally likely == uniform distribution, of course with the understanding that there isn't a total uniformness with floating point errors and the like. – Angs – 2018-05-18T16:54:49.287
@Angs or should same size area near zero have higher possibility as float precision is higher near zero? – l4m2 – 2018-05-18T17:10:16.380
3Requiring the interval to be closed or half-closed (as opposed to open) is a theoretically unobservable requirement. Many random number generators give the output in (0,1). How to test that the output interval is [ 0,1) and not (0,1)? The value 0 "never" occurs anyway – Luis Mendo – 2018-05-18T22:29:24.183
2Is it OK if our code uses rejection sampling, and so takes very long for test cases like
s=2.99999999999, n=3
? May we generate random reals in multiples of, say,1e-9
? – xnor – 2018-05-19T00:11:35.140@xnor Only if you have a supercomputer ("1 second"). | OP said 10 digits. – user202729 – 2018-05-19T03:27:26.740
@xnor none of the test cases may take longer than a second on your machine, so at least the most naive rejection sampling is out of the question. The implementation details don't matter if the output is correct. And like user202729 mentioned, 10 digits of precision is needed so multiples of 1e-10 or smaller work. – Angs – 2018-05-19T07:10:41.320
@LuisMendo the same applies to any number really, not just for 0. For instance,
Java.Random
has a 48-bit seed, so it can't produce all double-precision floating-point numbers ∈[0,1] since they have 52 bits of precision. Some common sense is required since it's hard to make a rule that would cover all cases. Maybe rounded to 10 digits after the comma, all cases should be equally common with an error margin of 1%. – Angs – 2018-05-19T07:23:04.397