随机选取10–100之间的10个且不重复的数字,存入一个数组并排
function randomRange(start, end, count) {
// 升序排序
function sortFunc(a, b) {
return a - b;
}
const randoms=[];
// 跳出while循环时 randoms数组有count个元素
while (randoms.length < count)
{
// 获取一个10–100范围的数
var random = Math.floor(Math.random()*(end - start + 1) + start);
// 判断当前随机数是否已经存在
if (!randoms.includes(random)) {
randoms.push(random);
}
}
randoms.sort(sortFunc);
return randoms;
}
randomRange(10, 100, 10);
本文标题:随机选取10–100之间的10个且不重复的数字,存入一个数组并排
本文链接:https://www.haomeiwen.com/subject/xpfjhktx.html
网友评论