获取一组随机下标(相邻两个不重复)的数组
function getRandomNumByRange(start, end) {
return Math.floor(Math.random() * (end - start) + start);
}
// 获取一组随机下标的数组
function getRandomCardBg(len) {
const res = [];
for (let i = 0; i < len; i++) {
const temp = this.getRandomNumByRange(0, 5);
if (i > 0) {
if (temp === res[i - 1]) {
for (let j = 0; j < 10000; j++) {
const temp2 = this.getRandomNumByRange(0, 4);
if (temp2 !== res[i - 1]) {
res.push(temp2);
break;
}
}
} else {
res.push(temp);
}
} else {
res.push(temp);
}
console.log('temp res', res)
}
return res;
}
var a = getRandomCardBg(10);
console.log('------ a', a);