【问题标题】:How to shuffle a list of lists in javascript [duplicate]如何在javascript中打乱列表列表[重复]
【发布时间】:2021-01-10 02:26:05
【问题描述】:

假设我有一个 Javascript 列表,如下所示:

var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]

我想随机化列表的第一级排序,而不是在列表内随机化。所以,例如,

shuffle(list_of_lists) 
> [["E", "B"], ["R", "R"], ["Y", "R"], ["N", "B"], ["U", "R"], ["A", "B"]]

是一种可能的随机洗牌,因为它在随机化顶级列表的同时保留了嵌套列表的顺序。

我尝试了许多不同的方法,但似乎无法进行改组。例如,这些标准的洗牌方法都不是:

var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]
      function shuffle(a) {
                var j, x, i;
                for (i = a.length - 1; i > 0; i--) {
                    j = Math.floor(Math.random() * (i + 1));
                    x = a[i];
                    a[i] = a[j];
                    a[j] = x;
                }
                return a;
            }
      
        function shuffle_2(array) {
            var currentIndex = array.length, temporaryValue, randomIndex;

            // While there remain elements to shuffle...
            while (0 !== currentIndex) {

                // Pick a remaining element...
                randomIndex = Math.floor(Math.random() * currentIndex);
                currentIndex -= 1;

                // And swap it with the current element.
                temporaryValue = array[currentIndex];
                array[currentIndex] = array[randomIndex];
                array[randomIndex] = temporaryValue;
            }

            return array;
        }

     list_of_list2 = list_of_list.sort(func);  
     function func(a, b) {  
                return 0.5 - Math.random();
            } 
            
            
console.log(list_of_list2.join("|"));
console.log(shuffle(list_of_list).join("|"));
console.log(shuffle_2(list_of_list).join("|"));

这样做的正确方法是什么?

【问题讨论】:

  • 你能详细说明“不起作用”吗?
  • 我把代码变成了一个 sn-p,它们都按照你解释的方式工作。
  • 嗯,他们似乎正在使用该代码 sn-p。也许我的问题在其他地方......对此感到抱歉
  • const shuffle = (arr = []) => { return arr.sort(() => 0.5 - Math.random()) }

标签: javascript jquery random shuffle


【解决方案1】:

    var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]];
    
    function shuffle(a){
         const arrlength = a.length;
         
         let shuffled = []; //array to be returned
         let numsused = []; //numbers chosen by rand
    
         while(shuffled.length < arrlength){
            let newnum = false;
            while(!newnum){
                const randm = Math.floor(Math.random() * arrlength);
                
                if(numsused.indexOf(randm) === -1){
                   shuffled.push(a[randm]);
                   newnum = true;
                   numsused.push(randm);
                }
            }
         }
         
         return shuffled;
    }
    
    console.log(shuffle(list_of_list));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多