Let \(X\) be the random variable giving the number of triangles in a graph drawn from \(G(n,p)\text{.}\) For \(1 \leq i \lt j \lt k \leq n\text{,}\) let \(X_{i,j,k}\) be the indicator random variable for the event that the vertices \(i, j, k\) form a triangle, i.e., that the edges \(\{i,j\},\{i,k\},\{j,k\}\) are all present in the graph. Then we have
\begin{equation*}
X = \sum_{1 \leq i \lt j \lt k \leq n} X_{i,j,k}.
\end{equation*}
By linearity of expectation, we have
\begin{equation*}
E[X] = \sum_{1 \leq i \lt j \lt k \leq n} E[X_{i,j,k}].
\end{equation*}
Now, for each triple \((i,j,k)\text{,}\) we have
\begin{equation*}
E[X_{i,j,k}] = P(X_{i,j,k} = 1) = p^3,
\end{equation*}
since for vertices \(i, j, k\) to form a triangle, all three edges connecting them must be present, and each edge is present with probability \(p\text{.}\) Thus, we have
\begin{equation*}
E[X] = \binom{n}{3} p^3.
\end{equation*}