I have the following math problem (writen in code):
var sum: CGFloat = 0
var sum2: CGFloat = 0
for _ in 0 ..< 10000000 { // This is a loop and runs 10000000 times
let x = CGFloat(Float(arc4random()) / Float(UINT32_MAX)) // Random between 0-1
let y = CGFloat(Float(arc4random()) / Float(UINT32_MAX)) // Random between 0-1
sum += abs(x-y) * abs(x-y) // abs() gives the absolute value
sum2 += abs((x-y))
}
var averageSum1: CGFloat = sum/10000000 // to get the average value from sum
var averageSum2: CGFloat = sum2/10000000 // to get the average value from sum2
1) print("\(averageSum1)") // = 0.166666.. = 1/6
2) print("\(averageSum2)") // = 0.333333.. = 1/3
3) print("\((averageSum2)*(averageSum2))") // = 0.11111.. = 1/9
For those who don't understand the code, we have \begin{align}\text{sum}&=\sum_{k=0}^{99999999}|x_k-y_k|^2\\ \text{sum2}&=\sum_{k=0}^{99999999}|x_k-y_k|\end{align}
Where $x_k$ and $y_k$ are random numbers in $[0,1]$
We calculate
\begin{align}\text{averageSum1}&=\frac{\text{sum}}{100000000}\\ \text{averageSum2}&=\frac{\text{sum2}}{100000000}\end{align}
We therefore have:
$\text{averageSum1}=\frac 16$
$\text{averageSum2}=\frac 13$
$\text{averageSum2}^2=\frac 19$
My question is, why is the result of 1) $\frac 16$ and not $\frac 19$ like in 3). I would think that $\frac 13 \times \frac 13 = \frac 19$. Even in a long loop? Has anyone an explanation for it?
Thank you in advance!
Consider if you just picked $0,1$ randomly, rather than a value in between. What would the average be of the difference? Half the time, the difference would be $1$ and half the time it would be zero, so the average would be $\frac{1}{2}$.
But the average of the square of the difference will be the same, $\frac{1}{2}$, because $0^2=0$ and $1^2=1$.
Your continuous random variables are attempting to estimate:
$$\int_{0}^1\int_{0}^1 |x-y|\,dx\,dy$$
and:
$$\int_{0}^1\int_{0}^1 |x-y|^2\,dx\,dy$$
You can actually compute these with some elementary calculus to get the values $\frac{1}{3}$ and $\frac{1}{6}$.