Created
March 28, 2025 16:41
-
-
Save dleshem/99e7e8cea3546374040269d22bd93e16 to your computer and use it in GitHub Desktop.
Calculates expected value of maximum of discrete uniform random variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rand = (min, max) => min + Math.floor((max - min + 1)*Math.random()); | |
const maxOf = (min, max, n) => { | |
let m = min; | |
for (let i = 0; i < n; ++i) { | |
m = Math.max(m, rand(min, max)); | |
} | |
return m; | |
} | |
const expectedMaxOf = (min, max, n, sample) => { | |
let sum = 0; | |
for (let i = 0; i < sample; ++i) { | |
sum += maxOf(min, max, n); | |
} | |
return sum / sample; | |
} | |
for (let i = 1; i <= 9; ++i) { | |
console.log(`Cutoff (draws left: ${i}): ${expectedMaxOf(1, 100, i, 1000000)}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment