Last active
November 30, 2017 20:58
-
-
Save audreybongalon/8d1d6b18727f3161e712d742a4542560 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/safufareqo
This file contains hidden or 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
| 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