Skip to content

Instantly share code, notes, and snippets.

@dtudury
Last active December 19, 2015 06:58
Show Gist options
  • Save dtudury/5914750 to your computer and use it in GitHub Desktop.
Save dtudury/5914750 to your computer and use it in GitHub Desktop.
chance of 2 campers having the same schedule assuming that all schedules are unique but 10% of campers incorrectly follow their schedule
//git clone https://gist.github.com/5914750.git
//node 5914750/nodeCampAttack
var classes = 8;
var schedules = 1; for(var i = 2; i <= classes; i++) schedules *= i; //8!
var mistakeRate = 0.1; //10% chance that a participant doesn't follow their schedule
var hitChance = 0.5; //what we're solving for
var missChance = 1; //100% (can't collide without participants)
var participant = 1;
while(missChance > 1 - hitChance) {
var usedSchedules = participant - 1;
var errorSchedules = usedSchedules * mistakeRate;
var notUsed = (schedules - usedSchedules) / schedules; //chance of selecting a schedule at random that's unused
var notMistaken = (schedules - errorSchedules) / schedules; //chance of selecting a schedule that's not used by a mistaken participant
var collisionChance = 0;
collisionChance += mistakeRate * notUsed; //if this participant is mistaken they can collide with anyone
collisionChance += (1 - mistakeRate) * notMistaken; //if this participant isn't mistaken they can only collide with mistaken participants
missChance *= collisionChance; //chance of missing up to this one is the chance of missing up to the last one times the chance of missing this one
console.log(participant + " " + (1 - missChance) * 100);
participant++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment