Let X be a random variable following uniform distribution with values lying between ‘a’ and ‘b’. The pdf of X is given by the formula, f(x) = \begin{cases} \frac{1}{b-a} & a \leq x \leq b \\ 0 & \text{otherwise}\end{cases}
The second moment of the random variable is given by the formula, E(X^2)= \frac{b^2+ba+a^2}{3}
Proof (Without using MGF) :
\begin{align*} E(X^2) &= \int_{a}^{b}x^2f(x)dx \\ &= \frac{1}{b-a}\int_{a}^{b}x^2dx \\ &= \frac{1}{b-a}\left[\frac{x^3}{3}\right]_a^b \\ &= \frac{1}{b-a}\left[\frac{b^3-a^3}{3}\right] \\ &= \frac{1}{b-a}\left[\frac{(b-a)(b^2 + ba +a^2)}{3}\right] \\ &=\frac{b^2+ba+a^2}{3} \end{align*}Proof (Using MGF) :
In order to find the second random moment of the uniform distribution using the MGF we should differentiate the moment generating function twice and then set t=0. The MGF (Moment Generating Function) of the uniform distribution is given as, M_X(t)= \frac {e^{tb}-e^{ta}}{t(b-a)} On differentiating it twice we get, \begin{align*} \frac{d^2}{dt^2}M_X(t) &= \frac{d^2}{dt^2}\left[ \frac {e^{tb}-e^{ta}}{t(b-a)}\right] \\ &= \frac{d^2}{dt^2}\left[ \frac {(1 + \frac{tb}{1!} + \frac{(tb)^2}{2!} + \frac{(tb)^3}{3!} + \frac{(tb)^4}{4!} + \ldots)-(1 + \frac{ta}{1!} + \frac{(ta)^2}{2!} + \frac{(ta)^3}{3!} + \frac{(ta)^4}{4!} + \ldots)}{t(b-a)}\right] \\ &= \frac{1}{b-a}\left[(b^3 + \frac{(tb)}{1} + \ldots)-(a^3 + \frac{(ta)}{1} + \ldots)\right] \end{align*}
Putting t=0 on both sides we get that, \begin{align*} E(X^2) = \frac{d^2}{dt^2}M_X(t) |_{t=0} &= \frac{1}{b-a}\left[(b^3 + \frac{(tb)}{1} + \ldots)-(a^3 + \frac{(ta)}{1} + \ldots)\right]_{t=0} \\ &= \frac{1}{b-a}\left[\frac{b^3-a^3}{3}\right] \\ &= \frac{1}{b-a}\left[\frac{(b-a)(b^2 + ba +a^2)}{3}\right] \\ &=\frac{b^2+ba+a^2}{3} \end{align*}