I wrote a simple piece of code to compare random array difference and found something ... that I don't quite understand.
I would have expected the result to be random number close to 0.5 but in practice, it is 0.3333.
Why does the random number array home in on 0.3 and not 0.5?
const result = document.getElementById('result');
const generateRandomNrArray = (nrNumbers) => {
let i;
let result = [];
for (i = 0; i < nrNumbers; i++) {
result.push(Math.random());
}
return result;
}
const getArrayDiff = (arr1, arr2) => {
var diff = 0;
arr1.forEach(function (v1, index) {
diff += Math.abs(v1 - arr2[index]);
});
return diff;
}
const run = (nr) => {
const arr1 = generateRandomNrArray(nr);
const arr2 = generateRandomNrArray(nr);
const totalDiff = getArrayDiff(arr1, arr2);
result.innerHTML = "Average difference:" + (totalDiff / nr);
}
button {font-size: 2em;}
<div id="result"></div>
<button id="run" onclick="run(1500)">Click Me</button>
This basically boils down to a limit and it makes sense. Consider the combinations of numbers between 0 and 10 and count the various differences you can make.
For example, there is one combination with a difference of 9 — (0, 9). There are 5 with a difference of 5:
[0, 5],
[1, 6],
[2, 7],
[3, 8],
[4, 9]
But there are nine combinations with a difference of 1:
[1, 2],
[2, 3],
...
[8, 9]
With 0 - 10 the counts are:
{1: 9, 2: 8, 3: 7, 4: 6, 5: 5, 6: 4, 7: 3, 8: 2, 9: 1}
There are 45 combinations and the average difference of the those combinations is 3.6666
not 5
because there are more smaller differences than larger ones.
When you increase the granularity from 0 - 10 to 0 - 100 the same pattern holds. There are 99 combinations that result in a difference 1 and only 50 with a difference of 50 for an average of 33.6666
.
As you increase the number of significant digits the opposite directions the opposite direction with finer and finer divisions between 0 and 1, you find the same process as the limit approaches 1/3
. There are many more smaller difference than larger ones pulling the average difference down. For 0-1
in 0.1 intervals you'll see 9 with a difference of 0.1 and 5 with a difference of 0.5, at 0.01 there will be 99 with a difference 0.01, and 50 with a difference of 0.5. As the interval approaches 0 the average of the differences approaches 1/3
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments