Optimize subset randomizer for interp
This commit is contained in:
@@ -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,11 +35,19 @@ export function takeSampleFrom(sourceList, amount) {
|
||||
return !randElements.includes(obj);
|
||||
});
|
||||
|
||||
if(swap) {
|
||||
return {
|
||||
sample: remainder,
|
||||
remainder: randElements
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
sample: randElements,
|
||||
remainder: remainder
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* With a circle radius r, and center at (h,k),
|
||||
|
||||
Reference in New Issue
Block a user