Home  >  Q&A  >  body text

js completely scrambles array - Stack Overflow

var arr = [0,1,2,3,4,5,6,7]

Completely disrupted, it is required that the position of each element must be changed

学习ing学习ing2662 days ago910

reply all(8)I'll reply

  • 仅有的幸福

    仅有的幸福2017-07-05 10:45:39

    First of all, the question is not about traditional array reordering. If it is traditional array reordering, of course Fisher-Yates is the first choice. The black technology of Math.random() cannot guarantee consistent probability. For details, you can see my previous post Analysis https://github.com/hanzichi/u...

    If every position needs to be changed, just write a piece of code and it will barely work:

    function shuffle(a) {
      let len = a.length;
      let shuffled = Array(len);
    
      for (let i = len; i--; ) {
        let rand = ~~(Math.random() * i);
        shuffled[i] = a[rand];
        a[rand] = a[i];
      }
    
      return shuffled;
    }

    Created a new array and polluted the original array, which is very inelegant. The subject can improve it by himself

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:45:39

    Putting the last one first will realize that every element is moved, but what does it mean to completely disrupt it?

    var arr = [0,1,2,3,4,5,6,7], last = arr[arr.length - 1];
    arr.splice(arr.length - 1, 1);
    arr.unshift(last) 

    reply
    0
  • 某草草

    某草草2017-07-05 10:45:39

    It is impossible to guarantee that every position will change randomly.

    If you want to completely disrupt it, you can’t guarantee that the positions will all change.

    The simpler method is to scramble it once and then compare it with the array until the conditions are met.

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 10:45:39

    刚没看清 , 这个可以吗?
    function upset(arr){
         let newArr = arr.slice().sort(() => Math.random()>0.5 ? 1 : -1)
         let result = arr.some((item,index)=>{
             return item === newArr[index]
         })
         if(!result){
             return newArr
         }else{
             return upset(newArr)
         }
    }
    

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:45:39

        function compare(a , b){
            Math.random > 0.5 ? return -1 ; return 1
        } 
        arr.sort(compare);
        

    reply
    0
  • 習慣沉默

    習慣沉默2017-07-05 10:45:39

    function shuffle (arr) {

        var _floor = Math.floor,
            _random = Math.random,
            len = arr.length;
        for(var i = 0; i < len - 1; ++i){
            var rand_pos = _floor(_random() * (len - i)) + i;
            if(rand_pos != i){
                var tmp = arr[i];
                arr[i] = arr[rand_pos];
                arr[rand_pos] = tmp;
            }
        }
        return arr;
    };

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:45:39

    Random panning, Caesar encryption is booming...

    function shuffle (arr) {
      if (!Array.isArray(arr)) { return [] }
    
      var len = Math.floor(Math.random() * (arr.length - 1)) + 1
      return arr.slice(len).concat(arr.slice(0, len))
    }

    reply
    0
  • 阿神

    阿神2017-07-05 10:45:39

    All the simplest positions can be moved

    > arr = [0,1,2,3,4,5,6,7]
    > arr.unshift(arr.pop())
    > arr
    [ 7, 0, 1, 2, 3, 4, 5, 6 ]
    > arr.sort((a , b)=>Math.random() > 0.5 ? -1:1)
    [ 6, 5, 4, 0, 1, 3, 7, 2 ]
    > arr.sort((a , b)=>Math.random() > 0.5 ? -1:1)
    [ 6, 0, 5, 3, 4, 7, 1, 2 ]

    All the positions are movedIn fact, it is not the most chaotic;

    The most chaotic thing is probably shuffling the cards randomly, with a certain probability of keeping a certain number unchanged, so there is no pattern to follow.

    reply
    0
  • Cancelreply