Last active
August 29, 2015 14:26
-
-
Save isner/6697e77d1492cf0236d9 to your computer and use it in GitHub Desktop.
Toilet seat touching simulation
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
var visitCount = 10000; | |
var touches = 0; | |
var seatDown = true; | |
/** | |
* The "Seat-Down" policy | |
* | |
* - Each user places the seat down after conducting | |
* his or her business. | |
* - `seatDown` need not be updated because the inital | |
* state of the seat is always down for each user. | |
*/ | |
for (var i = 0; i < visitCount; i++) { | |
var female = Math.random() > 0.5; // 50% | |
var num2 = Math.random() > 0.7; // 30% | |
if (female) { | |
if (seatDown) { | |
// noop | |
} | |
else { | |
touches += 1; | |
} | |
} | |
else { | |
if (seatDown) { | |
if (num2) { | |
// noop | |
} | |
else { | |
touches += 2; | |
} | |
} | |
else { | |
if (num2) { | |
touches += 1; | |
} | |
else { | |
// noop | |
} | |
} | |
} | |
} | |
/** | |
* "Seat-Down" results | |
*/ | |
console.log(); | |
console.log('Seat-down policy'); | |
console.log('Touches after 10000 visitors: %s', touches); | |
/** | |
* "Only-What-You-Need" policy | |
* | |
* - Each user only touches the seat when necessary | |
* to conduct his or her business. | |
* - `seatDown` is updated with each visit based on | |
* how the previous user left it. | |
*/ | |
touches = 0; | |
seatDown = true; | |
for (var i = 0; i < visitCount; i++) { | |
var female = Math.random() > 0.5; // 50% | |
var num2 = Math.random() > 0.7; // 30% | |
if (female) { | |
if (seatDown) { | |
seatDown = true; | |
} | |
else { | |
touches += 1; | |
seatDown = true; | |
} | |
} | |
else { | |
if (seatDown) { | |
if (num2) { | |
seatDown = true; | |
} | |
else { | |
touches += 1; | |
seatDown = false; | |
} | |
} | |
else { | |
if (num2) { | |
touches += 1; | |
seatDown = false; | |
} | |
else { | |
seatDown = false; | |
} | |
} | |
} | |
} | |
/** | |
* "Only-What-You-Need" results | |
*/ | |
console.log(); | |
console.log('Only-What-You-Need policy'); | |
console.log('Touches after 10000 visitors: %s', touches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment