Let $X_1, X_2, \dots, X_n \sim U([1,3])$. Write a code in $\mathcal{R}$ to find an approximate value of $$\mathbb{P}\left( \sum_{k=1}^n X_k< a \right)$$
I have seen multiple questions here where people asked about the sum of two or three uniformly distributed variables. There was one more that suggested to use Irwin-Hall distribution, but this distribution only works for $X_k \sim U([0,1])$.
My attempt so far was this (an example with $a=3$) :
k = 5
for(i in 1:k){
measurements <- runif(10, min = 1, max = 3) # take 10,000 measurements
above.threshold <- sum((measurements) < 3) #count the number of values < 4
rez <- above.threshold/length(measurements) # calculate the proportion of values < 4
}
This code doesn't work properly. I have calculated a couple of probabilities by hand:
$$\begin{equation} \begin{aligned} &\mathbb{P}(X_1+X_2<4)=\frac{1}{2}; \\ &\mathbb{P}(X_1+X_2+X_3<4)=\frac{1}{48}. \end{aligned} \end{equation}$$
Maybe there is someone who's familiar with $\mathcal{R}$ programming and has already faced this problem ? Thank you !
Here is how you can run the simulation with R:
$P(X_1 + X_2 < 4)$, where $X_1, X_2 \sim U(1, 3)$, prob. is $=\frac{1}{2}=0.5$
$P(X_1 + X_2 +X_3 < 4)$, where $X_1, X_2, X_3 \sim U(1, 3)$, prob. is $=\frac{1}{48}=0.02083333$