Skip to content

Instantly share code, notes, and snippets.

@rowinf
Created November 23, 2017 07:51
Show Gist options
  • Save rowinf/06b0990f0efe1fe414a5308623332b66 to your computer and use it in GitHub Desktop.
Save rowinf/06b0990f0efe1fe414a5308623332b66 to your computer and use it in GitHub Desktop.
function getRandom(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
const generate = () => {
const selected = []
let max = 9
let prev = null
for (let i = 0; i < 4; i++) {
const min = i === 0 ? 1 : 0
let digit = getRandom(min, max)
while (digit === prev || digit === prev + 1) {
digit = getRandom(min, max)
}
selected.push(digit)
prev = digit
}
// convert array to a 4 digit number
const number = selected.reduce((total, i, idx) => total + (i * 10 ** (3 - idx)), 0)
return number
}
const numberSet = {}
const generated = []
const count = 1000
while (generated.length < count) {
num = generate()
if (!numberSet[num]) {
numberSet[num] = true
generated.push(num)
}
}
// console.log(generated.length)
console.log(generated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment