Remove and fix comments

This commit is contained in:
Joke Book 2019-03-11 21:56:58 +00:00
parent 43faf2a64d
commit 7e23168913
No known key found for this signature in database
GPG key ID: FE1A37AA1630F6EF

View file

@ -79,42 +79,20 @@ class MathUtil {
} }
/** /**
* Return a random number given an inclusive range and a set * Return a random number given an inclusive range and a number in that
* of numbers that should be excluded. * range that should be excluded.
* For instance, (1, 5, [2, 3]) will only pick 1, 4, or 5 (with equal *
* For instance, (1, 5, 3) will only pick 1, 2, 4, or 5 (with equal
* probability) * probability)
* *
* @param {number} lower - The lower bound (inlcusive) * @param {number} lower - The lower bound (inlcusive)
* @param {number} upper - The upper bound (inclusive), such that lower <= upper * @param {number} upper - The upper bound (inclusive), such that lower <= upper
* @param {number} exclude - The numbers to exclude (MUST be in the range) * @param {number} exclude - The number to exclude (MUST be in the range)
*/ */
static inclusiveRandIntWithout(lower, upper, excluded) { static inclusiveRandIntWithout(lower, upper, excluded) {
/** // Note that subtraction is the number of items in the
* By excluding a random integer, we are lowering the number // inclusive range [lower, upper] minus 1 already
* of possible options by one. // (e.g. in the set {3, 4, 5}, 5 - 3 = 2)
*
* The algorithm is to pick a random integer in the range
* [lower, upper-1]. If we accidentally pick the exlcuded number,
* we will instead pikc "upper".
*
* So if C is excluded, and we are picking A..F then:
*
* A B C D E (no F)
* = = = = =
* A B F D E
*
* In the event that our excluded number isn't in the range, we will never pick it,
* and so we don't need to worry about remapping it to something else. For example,
* if we want to exclude 'F':
*
* A B C D E (no F)
* = = = = =
* A B C D E
*/
// Note that 5 - 3 = 2, but there are 3 options (3, 4, 5).
// As a result, there isn't a need to subtract 1 as we've
// already got one less than the size of the range.
const possibleOptions = upper - lower; const possibleOptions = upper - lower;
const randInt = lower + Math.floor(Math.random() * possibleOptions); const randInt = lower + Math.floor(Math.random() * possibleOptions);