Skip to content

Instantly share code, notes, and snippets.

@dleshem
Created March 28, 2025 16:41
Show Gist options
  • Save dleshem/99e7e8cea3546374040269d22bd93e16 to your computer and use it in GitHub Desktop.
Save dleshem/99e7e8cea3546374040269d22bd93e16 to your computer and use it in GitHub Desktop.
Calculates expected value of maximum of discrete uniform random variables
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