TITLE: Calculate expected value programmed Y
QUESTION [6 upvotes]: Let Random Variable Y be determined by the following algorithm:

Initially Y = 0;
While Y < 1, random number (uniformly distributed in-between [0;1]) is being returned.

In Python code would look like this:
Y = 0
while Y < 1:
    Y += random()

The task is to find Y expected value.
What I've managed to do - just sampling with 1e8 iterations, getting a result of somewhere near 1.39. However the task is to find it using maths. Do you have any ideas?

REPLY [2 votes]: Let $P_n(x)\ (0\leq x<1)$ be the probability density for $x=Y$ after n iterations. Since we add uniformly random numbers, the probability at some $x$ is the integral of the probability in the previous  range $[x-1,x]$, namely, $[0,x]$.
$$P_0(x)=1,\ P_{n+1}(x)=\int_0^x P_n(t)\,dt$$
Recursively,
$$P_{1}(x)=x,\ P_{2}(x)=\int_0^xP_1(t)\,dt=\frac{x^2}{2},\ \ldots,\ P_n(x)=\frac{x^n}{n!}$$
Likewise, the probability density for $x=Y$ for the final $Y$ is the sum of integrals of $P_{n-1}$ in the range $[x-1,x]$, namely, $[x-1,1]$ because $1\leq x\leq 2$.
And since $e^x=\sum_{n=0}^\infty \frac{x^n}{n!}$,
$$Q(x)=\sum_{n=1}^\infty\int_{x-1}^1P_{n-1}(t)\,dt=\sum_{n=1}^\infty\left(\frac{1}{n!}-\frac{(x-1)^n}{n!}\right)=(e^1-1)-(e^{x-1}-1)=e-e^{x-1}$$
Therefore, the expected value for $Y$ is
$$\int_1^2xQ(x)\,dx=\frac{e}{2}\approx1.359$$