Skip to content

Instantly share code, notes, and snippets.

@Jimbly
Last active July 27, 2020 20:49
Show Gist options
  • Save Jimbly/9364e1bd50877357c74d8ff5d3631ac1 to your computer and use it in GitHub Desktop.
Save Jimbly/9364e1bd50877357c74d8ff5d3631ac1 to your computer and use it in GitHub Desktop.
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