Looking for a way to compute the discrete version of a continuous random distribution I just learn the way to compute the cumulative probability distribution (CDF) of a distribution in terms of some known results. For example, a random variable $Y$ is written in terms of a uniform random variable $U(0,1)$ as
$$
  Y \sim \frac{5}{U + 1}
$$
the CDF of $Y$ could be found in the following way
$$
  \begin{align*}
  \text{CDF}(y) & = F(y) = \mathbb{P}(Y\leq y) = \mathbb{P}\left(\frac{5}{U+1}\leq y\right)\\
  & = \mathbb{P}\left(U\geq \frac{5}{y}-1\right) = 1- \mathbb{P}(U<\frac{5}{y}-1)\\
  & = 2 - \frac{5}{y}
  \end{align*}
$$
so the probability distribution function (PDF) for $Y$ will be
$$
  P(y) = \dfrac{dF(y)}{dy} = \frac{5}{y^2}
$$
Compute the expectation value for $y$
$$
  \text{E}[y] = \int y\frac{5}{y^2}dy = 3.46574
$$
In the text, it is said that the uniform distribution is continuous so I apply the integral to compute the expectation. I read something online about discrete distribution. If I change the random variable as
$$
  Y \sim \left\lfloor \frac{5}{U+1} \right\rfloor
$$
how do we derive the PDF and CDF for the discrete version of $Y$? I am trying a simulation to run 1M random numbers for u as follows:
  s = 0
  for i=1 to 1000000
    s += floor(5/(rand(0, 1) + 1)
  end
  print s/1000000

this gives me an average of 2.9172 instead of 3.46574 I think the difference is originated from a great number of states that are suppressed by the floor operation. I wonder if there is any analytical way instead of a simulation to compute the result. I try to replace y in PDF with the floor(5/(u+1)) and replace the integral with a summation
$$
  \sum \left\lfloor\frac{5}{y+1}\right\rfloor \times \frac{5}{\left\lfloor\frac{5}{y+1}\right\rfloor^2} = \sum \frac{5}{\left\lfloor\frac{5}{y+1}\right\rfloor}
$$
but this gives me 1.8418, very far from 2.9172.

Since you calculated the original expected value correctly, you already know this, but just to be thorough for other readers, the CDF in the question is not properly specified, as $P(U < \frac{5}{y} - 1)$ is only $\frac{5}{y} - 1$ if this quantity is between 0 and 1. Otherwise, the probability is either 0 or 1. The appropriate range for $y$ where your logic holds is
$$ 0 \le \frac{5}{y} - 1 \le 1 \Longrightarrow y \le 5 \le 2y \Longrightarrow \frac{5}{2} \le y \le 5;$$
if $y < \frac{5}{2}$, we have $P(U < \frac{5}{y} - 1) = 1$ and if $y > 5$, we have $P(U < \frac{5}{y} - 1) = 0$.
Thus, we have
$$
\begin{align*}
CDF(y) &= P(Y \le y) \\
&= P(\frac{5}{U + 1} \le y) \\
&= P(U \ge \frac{5}{y} - 1) \\
&= 1 - P(U < \frac{5}{y} - 1) \\
&= \begin{cases} 0, & y < \frac{5}{2}, \\ 2 - \frac{5}{y}, & \frac{5}{2} \le y \le 5, \\ 1, & y > 5. \end{cases}
\end{align*}
$$
The PDF follows as
$$PDF(y) = \frac{d (CDF(y))}{dy} = \begin{cases} \frac{5}{y^2}, & \frac{5}{2} \le y \le 5, \\ 0, & o/w. \end{cases}$$
And the expected value follows as
$$E[Y] = \int_{-\infty}^{\infty} y \cdot PDF(y) dy = \int_{\frac{5}{2}}^{5} y \cdot \frac{5}{y^2} dy = 5\ln(5) - 5 \ln(5/2) \approx 3.466,$$
as you calculated.
For the discrete case, we can just do casework on where U falls:

*

*If $U = 0$, we have $Y = 5$.

*If $0 < U \le \frac{1}{4}$, we have $Y = 4$.

*If $\frac{1}{4} < U \le \frac{2}{3}$, we have $Y = 3$.

*If $\frac{2}{3} < U \le 1$, we have $Y = 2$.

The probability mass function for the discrete Y is then
$$P(Y = y) = \begin{cases} \frac{1}{3}, & y = 2, \\ \frac{5}{12}, & y = 3, \\ \frac{1}{4}, & y = 4. \end{cases} $$
This yields an expected value of
$$E[Y] = \sum y \cdot P(Y = y) = 2 \cdot \frac{1}{3} + 3 \cdot \frac{5}{12} + 4 \cdot \frac{1}{4} = \boxed{\frac{35}{12}},$$
which is around 2.917.
