美文网首页
随机选取10–100之间的10个且不重复的数字,存入一个数组并排

随机选取10–100之间的10个且不重复的数字,存入一个数组并排

作者: zhulichao | 来源:发表于2020-07-24 09:05 被阅读0次
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