Created
November 24, 2017 16:57
-
-
Save noppanit/862b8f003c4db2ca205ef99ad50682b6 to your computer and use it in GitHub Desktop.
Sampling data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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