js completely scrambles array - Stack Overflow
学习ing
学习ing 2017-07-05 10:43:39
0
8
909
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
学习ing

reply all(8)
仅有的幸福

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

洪涛

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) 
某草草

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.

巴扎黑
刚没看清 , 这个可以吗?
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)
     }
}
Ty80
    function compare(a , b){
        Math.random > 0.5 ? return -1 ; return 1
    } 
    arr.sort(compare);
    
習慣沉默

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;
};
我想大声告诉你

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))
}
阿神

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!