From 071bea1a8d27e953fee1f41f8fa7a7cb4ea9c2a0 Mon Sep 17 00:00:00 2001 From: Pitchaya Boonsarngsuk <2285135b@student.gla.ac.uk> Date: Mon, 5 Feb 2018 10:56:03 +0000 Subject: [PATCH] Optimize subset randomizer for interp --- src/interpolation/helpers.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/interpolation/helpers.js b/src/interpolation/helpers.js index 92c6614..2bf620c 100644 --- a/src/interpolation/helpers.js +++ b/src/interpolation/helpers.js @@ -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); }); - return { - sample: randElements, - remainder: remainder - }; + if(swap) { + return { + sample: remainder, + remainder: randElements + }; + } + else { + return { + sample: randElements, + remainder: remainder + }; + } } /**