Finding dynamic probabilities in R I have a sequence of independent uniform random variables $X_1,X_2,X_3,... \sim \text{IID U}(1,3)$ and I need to compute the value:
$$\sum_{n=1}^\infty \mathbb{P} \Bigg( \sum_{k=1}^n X_k < a \Bigg).$$
The biggest issue that I faced here is that I don't know how to write a script in R to find the inner sum.  What I have is:
k = 10000
x <- vector(length = k)
for(i in 1:k){
    measurements <- runif(10000, min = 1, max = 3) # take 10,000 measurements
    above.threshold <- sum(measurements < 4) #count the number of values < 4
    rez <- above.threshold/length(measurements) # calculate the proportion of values < 4
    x[i] <- rez
}
sum(x)/k

The code is obviously not right because I don't know how to code the inner sum so that the sum of uniformly distributed random variables would be less than some fixed number.
I have googled a lot but haven't found a lot. Any help would be great!
 A: As a preliminary observation, it is worth noting that the quantity of interest here can be simplified to a finite sum.  Since $X_k \geqslant 1$ for all $k \in \mathbb{N}$ you have $\sum_{k=1}^n X_k \geqslant n$ which implies that:
$$\mathbb{P} \Bigg( \sum_{k=1}^n X_k < a \Bigg) = 0
\quad \quad \quad \text{for all } n \geqslant a.$$
Thus, for any $a \in \mathbb{R}$ you can simplify the quantity of interest to the finite sum:
$$\sum_{n=1}^\infty \mathbb{P} \Bigg( \sum_{k=1}^n X_k < a \Bigg)
= \sum_{n=1}^{\lfloor a \rfloor} \mathbb{P} \Bigg( \sum_{k=1}^n X_k < a \Bigg).$$
(You can also simplify some of the case on the other end using the upper bound of the random variables.)  Now, the sum of independent uniform random variables has a generalised Irwin-Hall distribution.  With the bounds for your uniform distribution you have:
$$\sum_{k=1}^n \frac{X_k-1}{2} \sim \text{IH}(n).$$
Exact computation of the inner probabilities is possible if $n$ is not too large, however it is tricky for large $n$ due to underflow issues involved in computing the CDF of the Irwin-Hall distribution.  You can use the central limit theorem to approximate the Irwin-Hall distribution by the normal distribution when $n$ is large, and this gives a simple and effective approximation.

In your question you are computing an empirical estimate from simulation, which is one approach you can take.  I will show you one way you can do this.
Taking a large number of simulations $S$ we can approximate the quantity of interest as:
$$\sum_{n=1}^\infty \mathbb{P} \Bigg( \sum_{k=1}^n X_k < a \Bigg) \approx
\frac{1}{S} \sum_{s=1}^S \sum_{n=1}^{\lfloor a \rfloor} \mathbb{I} \Bigg( \sum_{k=1}^n X_k^{(s)} < a \Bigg).$$
This can be done using the following code in R.  In this code I generate a matrix of uniform random variables to cover all the simulations and then compute the indicators from these values.
compute.quantity <- function(a, sims = 1e6) {
  
  #Deal with trivial case
  A <- floor(a)
  if (A < 1) { return(0) }
  
  #Generate simulations
  X <- matrix(runif(sims*A, 1, 3), nrow = sims, ncol = A)
  
  #Compute approximation
  IND <- (apply(X, MARGIN = 1, FUN = 'cumsum') < a)
  sum(IND)/sims }

Here are some simulated values from the function.
#Simluate some approximate values of the quantity
set.seed(1)
compute.quantity(10)
[1] 4.542582

compute.quantity(20)
[1] 9.539944

compute.quantity(50)
[1] 24.54417

compute.quantity(100)
[1] 49.53875

