JSFiddle - React, Tailwind, and code Playground
by brigand
JavaScript
function randInt(min, max, exclude) {
const size = max - min - exclude.filter(x => x >= min && x < max).length;
let n = Math.floor(Math.random() * size) + min;
for (const ex of exclude) {
if (n === ex) {
n = ex + 1;
}
}
return n;
}
{
const min = 5;
const max = 10;
const exclude = [ 4, 5, 7];
const items = Array.from({
length: 1000
}, () => randInt(min, max, exclude))
console.log(items)
const counts = new Map(Array.from({
length: 5
}, (x, i) => [i + min, 0]));
items.forEach(item => {
counts.set(item, counts.get(item) + 1);
})
console.log(counts)
}