Skip to content

Instantly share code, notes, and snippets.

@audreybongalon
Last active November 30, 2017 20:58
Show Gist options
  • Select an option

  • Save audreybongalon/8d1d6b18727f3161e712d742a4542560 to your computer and use it in GitHub Desktop.

Select an option

Save audreybongalon/8d1d6b18727f3161e712d742a4542560 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/safufareqo
var POPULATION_SIZE = 10;
var SAMPLE_SIZE = 5;
var nums = [];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
// the maximum is exclusive and the minimum is inclusive
}
function inArray(num, arr) {
for (let item of arr) {
if (num == item) {
return true;
}
}
return false;
}
while (nums.length < SAMPLE_SIZE) {
var option = getRandomInt(1, POPULATION_SIZE);
if (!inArray(option, nums)) {
nums.push(option);
}
}
// sort numerically
nums.sort(function(a, b) {
return a - b;
});
console.log(nums);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment