Make randomize clearer.

This commit is contained in:
Howard Stearns 2016-02-10 12:53:05 -08:00
parent 31d72ffb8d
commit 989192e9fd

View file

@ -28,11 +28,11 @@
function virtualBatonf(options) { function virtualBatonf(options) {
// Answer averages (number +/- variability). Avoids having everyone act in lockstep. // Answer averages (number +/- variability). Avoids having everyone act in lockstep.
function randomize(number, variability) { function randomize(number, variability) {
var allowedDeviation = number * variability; var allowedDeviation = number * variability; // one side of the deviation range
var theNumberThatIsTwice = 2; var allowedDeviationRange = allowedDeviation * 2; // total range for +/- deviation
// random() is (0, 1], averages 0.5. The average of twice that is 1. var randomDeviation = Math.random() * allowedDeviationRange;
var randomDeviation = Math.random() * theNumberThatIsTwice * allowedDeviation; var result = number - allowedDeviation + randomDeviation;
return number - allowedDeviation + randomDeviation; return result;
} }
// Allow testing outside in a harness outside of High Fidelity. // Allow testing outside in a harness outside of High Fidelity.
// See sourceCodeSandbox/tests/mocha/test/testVirtualBaton.js // See sourceCodeSandbox/tests/mocha/test/testVirtualBaton.js