mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Remove and fix comments
This commit is contained in:
parent
43faf2a64d
commit
7e23168913
1 changed files with 8 additions and 30 deletions
|
@ -79,42 +79,20 @@ class MathUtil {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return a random number given an inclusive range and a set
|
||||
* of numbers that should be excluded.
|
||||
* For instance, (1, 5, [2, 3]) will only pick 1, 4, or 5 (with equal
|
||||
* Return a random number given an inclusive range and a number in that
|
||||
* range that should be excluded.
|
||||
*
|
||||
* For instance, (1, 5, 3) will only pick 1, 2, 4, or 5 (with equal
|
||||
* probability)
|
||||
*
|
||||
* @param {number} lower - The lower bound (inlcusive)
|
||||
* @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) {
|
||||
/**
|
||||
* By excluding a random integer, we are lowering the number
|
||||
* of possible options by one.
|
||||
*
|
||||
* 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.
|
||||
// Note that subtraction is the number of items in the
|
||||
// inclusive range [lower, upper] minus 1 already
|
||||
// (e.g. in the set {3, 4, 5}, 5 - 3 = 2)
|
||||
const possibleOptions = upper - lower;
|
||||
|
||||
const randInt = lower + Math.floor(Math.random() * possibleOptions);
|
||||
|
|
Loading…
Reference in a new issue