# Round, Round, Get a Round, I Get a Round

## This Week's Fiddler

Let's consider the *error* of a number being the difference between the rounded result and the number itself. So $0.4$ would have an error of $-0.4$, and $0.7$ would have an error of $0.3$. The error is thus uniformly distributed from $(-0.5, 0.5]$, where any $x\lt0.5$ incurs an error of $x$, and any $x\ge0.5$ incurs an error of $1-x$.

Now let's consider summing two random numbers. The errors are additive. For example, $0.4+0.3$ would have a combined error of $-0.4-0.3=-0.7$. Its sum of rounded numbers would add to $0+0=0$, but its rounded sum would be rounding $0.4+0.3=0.7$ to $1$. So the combined error must rest between $-0.5$ and $0.5$ for the sum of rounds to agree with the rounded sum.

In other words, the question is asking what the probability is for two independent and identically distributed (i.i.d.) uniform distributions from $-0.5$ to $0.5$ to have a sum between $-0.5$ and $0.5$. As the sum of two uniform distributions leads to a triangular shape, we can find the proportion of the triangle that rests between $x=-0.5$ and $x=0.5$ and divide it by the area of the entire triangle, which ranges from $x=-1$ to $x=1$ (the bounds of the distribution). Due to symmetry, we can find the area taken up from $x=-0.5$ to $x=0$ and divide that by the area from $x=-1$ to $x=0$.

Mental-math-wise, the left side is a right triangle similar to the larger triangle, with a base scaled by $0.5$. So its area should be $0.25$ that of the larger triangle, making the area we're looking for $1-0.25=0.75$.

This means the probability is 75%.

**Answer**: 75%

## Extra Credit

Continuing from before, the sum of errors for $N$ rounded random variables would actually be the sum of $N$ i.i.d. uniformly distributed variables from $0.5$ to $0.5$. The Irwin–Hall distribution describes this sum of uniformly distributed variables, albeit for $U(0,1)$ (we'll adjust accordingly).

$f_X(x;N)=\frac{1}{(N-1)!}\sum_{k=0}^{\lfloor x\rfloor}(-1)^k{N\choose k}(x-k)^{N-1}$This distribution goes from $0$ to $N$, unlike our distribution from the previous part that would go from $\frac{-N}{2}$ to $\frac{N}{2}$. Because we are trying to find the probability that the sum of rounds matches the rounded sum, we want to find the integral of this value in the middle of its distribution, namely the middle from $\frac{N-1}{2}$ to $\frac{N+1}{2}$. Due to this distributions symmetry, we can simply take the first half of this (integrating from $\frac{N-1}{2}$ to $\frac{N}{2}$) and double it. We're left with the integral:

$\begin{align*} 2\int_{\frac{N-1}{2}}^{\frac{N}{2}}f_X(x;N)\ \textrm{d}x&=2\int_{\frac{N-1}{2}}^{\frac{N}{2}}\frac{1}{(N-1)!}\sum_{k=0}^{\lfloor x\rfloor}(-1)^k{N\choose k}(x-k)^{N-1}\ \textrm{d}x \\ &=\frac{2}{(N-1)!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\int_{\frac{N-1}{2}}^{\frac{N}{2}}(x-k)^{N-1}\ \textrm{d}x \\ &=\frac{2}{(N-1)!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\biggl[\frac{1}{N}(x-k)^N\biggr]_{\frac{N-1}{2}}^{\frac{N}{2}} \\ &=\frac{2}{N!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\biggl(\biggl(\frac{N}{2}-k\biggr)^N-\biggl(\frac{N-1}{2}-k\biggr)^N\biggr) \\ \end{align*}$This was the closest I could get to a closed form. At the very least, it's easy to compute! The following computes and simulates the probabilities for $N$ from $1$ to $10$.

## Code

```
import math
import random
from fractions import Fraction
random.seed(12)
T = 10**6
for n in range(1, 11):
print(f'{n=}')
expected_prob = Fraction(2, math.factorial(n)) * sum(
(-1) ** k
* math.comb(n, k)
* ((Fraction(n, 2) - k) ** n - (Fraction(n - 1, 2) - k) ** n)
for k in range(0, (n + 1) // 2)
)
print(f'{expected_prob=!s} ({float(expected_prob)})')
successes = 0
for _ in range(T):
nums = [random.random() for _ in range(n)]
successes += round(sum(nums)) == sum(round(num) for num in nums)
simulated_prob = successes / T
print(f'{simulated_prob=}')
print()
```

## Output

```
n=1
expected_prob=1 (1.0)
simulated_prob=1.0
n=2
expected_prob=3/4 (0.75)
simulated_prob=0.749818
n=3
expected_prob=2/3 (0.6666666666666666)
simulated_prob=0.666279
n=4
expected_prob=115/192 (0.5989583333333334)
simulated_prob=0.597873
n=5
expected_prob=11/20 (0.55)
simulated_prob=0.550337
n=6
expected_prob=5887/11520 (0.5110243055555556)
simulated_prob=0.510976
n=7
expected_prob=151/315 (0.4793650793650794)
simulated_prob=0.47955
n=8
expected_prob=259723/573440 (0.4529209681919643)
simulated_prob=0.452742
n=9
expected_prob=15619/36288 (0.4304177689594356)
simulated_prob=0.430889
n=10
expected_prob=381773117/928972800 (0.4109626428244185)
simulated_prob=0.410581
```

$N$ | Probability of success | Approx. |
---|---|---|

1 | $1$ | 100% |

2 | $\frac{3}{4}$ | 75% |

3 | $\frac{2}{3}$ | 66.67% |

4 | $\frac{115}{192}$ | 59.79% |

5 | $\frac{11}{20}$ | 55.03% |

6 | $\frac{5887}{11520}$ | 51.10% |

7 | $\frac{151}{315}$ | 47.96% |

8 | $\frac{259723}{573440}$ | 45.27% |

9 | $\frac{15619}{36288}$ | 43.09% |

10 | $\frac{381773117}{928972800}$ | 41.06% |

**Answer**: 2/N! * sum from k=0 to floor((N - 1) / 2) for (-1)^k * (N choose k) * ((N / 2 - k)^N - ((N - 1) / 2 - k)^N)