Skip to content

Instantly share code, notes, and snippets.

@Jimbly
Last active Jul 27, 2020
Embed
What would you like to do?
const { random, floor } = Math;
function tryit() {
let count = 0;
while (true) {
++count;
if (random() < 1 / (2000 - count)) {
return count;
}
}
}
let data = [];
for (let ii = 0; ii < 10000; ++ii) {
let num_rolls = tryit();
let bucket = floor(num_rolls / 200);
data[bucket] = (data[bucket] || 0) + 1;
}
for (let ii = 0; ii < data.length; ++ii) {
console.log(`${ii*200}-${(ii+1)*200}: ${data[ii] || 0}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment