美文网首页
js 乱序数组

js 乱序数组

作者: 杰克_王_ | 来源:发表于2023-05-20 10:25 被阅读0次
    var times = 1000;
    var numLen = times.toString().length;
    var indexMulti = Math.pow(10, numLen);
    console.log(indexMulti);
    var sourceArray = [];
    for (var i = 0; i < times; i++) {
        sourceArray.push(i);
    }
    console.log(sourceArray);
    var index = times;
    while (index > 0) {
        var _index = Math.trunc(Math.random() * indexMulti % index);
        // debugger
        var data = sourceArray[_index];
        sourceArray[_index] = sourceArray[index - 1];
        sourceArray[index - 1] = data;
        index--;
    }
    console.log(sourceArray);
    

    相关文章

      网友评论

          本文标题:js 乱序数组

          本文链接:https://www.haomeiwen.com/subject/sucosdtx.html