Optimize subset randomizer for interp
This commit is contained in:
@@ -10,12 +10,19 @@
|
|||||||
*/
|
*/
|
||||||
export function takeSampleFrom(sourceList, amount) {
|
export function takeSampleFrom(sourceList, amount) {
|
||||||
let randElements = [],
|
let randElements = [],
|
||||||
max = sourceList.length;
|
max = sourceList.length,
|
||||||
|
swap = false;
|
||||||
|
|
||||||
if (amount >= max) {
|
if (amount >= max) {
|
||||||
return {sample: sourceList, remainder: {}};
|
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) {
|
for (let i = 0; i < amount; ++i) {
|
||||||
let rand = sourceList[Math.floor((Math.random() * max))];
|
let rand = sourceList[Math.floor((Math.random() * max))];
|
||||||
// Re-random until suitable value is found.
|
// Re-random until suitable value is found.
|
||||||
@@ -28,10 +35,18 @@ export function takeSampleFrom(sourceList, amount) {
|
|||||||
return !randElements.includes(obj);
|
return !randElements.includes(obj);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
if(swap) {
|
||||||
sample: randElements,
|
return {
|
||||||
remainder: remainder
|
sample: remainder,
|
||||||
};
|
remainder: randElements
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return {
|
||||||
|
sample: randElements,
|
||||||
|
remainder: remainder
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user