Discover a potential solution utilizing ES6 Set ("an array that contains only unique values").
Examples of how to use:
// Obtain 4 unique random numbers: from 0 to 4 (inclusive):
getUniqueNumbersInRange(4, 0, 5) //-> [5, 0, 4, 1];
// Obtain 2 unique random numbers: from -1 to 2 (inclusive):
getUniqueNumbersInRange(2, -1, 2) //-> [1, -1];
// Obtain 0 unique random numbers (empty result): from -1 to 2 (inclusive):
getUniqueNumbersInRange(0, -1, 2) //-> [];
// Obtain 7 unique random numbers: from 1 to 7 (inclusive):
getUniqueNumbersInRange(7, 1, 7) //-> [ 3, 1, 6, 2, 7, 5, 4];
The code snippet:
function getUniqueNumbersInRange(uniqueNumbersCount, fromInclusive, untilInclusive) {
// Step 0/3: Validate inputs.
if (0 > uniqueNumbersCount) throw new Error('The number of unique numbers cannot be negative.');
if (fromInclusive > untilInclusive) throw new Error('"From" bound "' + fromInclusive
+ '" cannot be greater than "until" bound "' + untilInclusive + '".');
const rangeLength = untilInclusive - fromInclusive + 1;
if (uniqueNumbersCount > rangeLength) throw new Error('The length of the range is ' + rangeLength + '=['
+ fromInclusive + '…' + untilInclusive + '] which is smaller than '
+ uniqueNumbersCount + ' (specified count of result numbers).');
if (uniqueNumbersCount === 0) return [];
// Step 1/3: Create a new "Set" – an object that holds unique values.
const uniqueDigits = new Set();
// Step 2/3: Populate with random numbers.
while (uniqueNumbersCount > uniqueDigits.size) {
const nextRngNmb = Math.floor(Math.random() * rangeLength) + fromInclusive;
uniqueDigits.add(nextRngNmb);
}
// Step 3/3: Convert "Set" with unique numbers into an array using "Array.from()".
const resArray = Array.from(uniqueDigits);
return resArray;
}
The advantages of this implementation:
- Includes a basic validation of input arguments – preventing unexpected outcomes when the range is too small, etc.
- Allows for a negative range (not limited to starting from 0), such as generating random numbers from -1000 to 500.
- Desired behavior: Unlike other solutions, this one does not automatically extend the range if it's too small. For example, requesting 10000 unique numbers within a range from 0 to 10 should trigger an error due to insufficient range (only 11 possible unique numbers). This implementation does not secretly expand the range up to 10000.