Skip to content

Instantly share code, notes, and snippets.

@kanian
Created May 21, 2019 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanian/8fb2e815ea1c2a3a82b70b91ef75d1bf to your computer and use it in GitHub Desktop.
Save kanian/8fb2e815ea1c2a3a82b70b91ef75d1bf to your computer and use it in GitHub Desktop.
function randomNumberInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomNumberSequence(min, max, n) {
return randomFiller(min, max, n).next().value;
}
function* randomFiller(min, max, n) {
if(n > (max - min + 1)){ // it's mathematically impossible
return undefined
}
let rands = [];
let current;
while (n > 0) {
while (rands.indexOf((current = randomNumberInRange(min, max))) !== -1) {
current = randomNumberInRange(min, max);
}
rands.push(current);
n--;
}
yield rands;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment