Optimize subset randomizer for interp

This commit is contained in:
Pitchaya Boonsarngsuk
2018-02-05 10:56:03 +00:00
parent a512831377
commit 071bea1a8d

View File

@@ -10,12 +10,19 @@
*/
export function takeSampleFrom(sourceList, amount) {
let randElements = [],
max = sourceList.length;
max = sourceList.length,
swap = false;
if (amount >= max) {
return {sample: sourceList, remainder: {}};
}
// If picking more than half of the entire set, random to pick the remainder instead
if (amount > Math.ceil(max/2)){
amount = max - amount;
swap = true;
}
for (let i = 0; i < amount; ++i) {
let rand = sourceList[Math.floor((Math.random() * max))];
// Re-random until suitable value is found.
@@ -28,10 +35,18 @@ export function takeSampleFrom(sourceList, amount) {
return !randElements.includes(obj);
});
if(swap) {
return {
sample: remainder,
remainder: randElements
};
}
else {
return {
sample: randElements,
remainder: remainder
};
}
}
/**