Skip to content

Instantly share code, notes, and snippets.

@noppanit
Created November 24, 2017 16:57
Show Gist options
  • Save noppanit/862b8f003c4db2ca205ef99ad50682b6 to your computer and use it in GitHub Desktop.
Save noppanit/862b8f003c4db2ca205ef99ad50682b6 to your computer and use it in GitHub Desktop.
Sampling data
const samples = [];
sampleSize = 5;
records = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
records.forEach((record, index) => {
if (samples.length < sampleSize) {
samples.push(record);
} else {
let rand = Math.floor(Math.random() * index);
if (rand < sampleSize) {
samples[rand] = record;
}
}
});
console.log(samples);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment