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
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.
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:
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?
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 shuffle (arr) {
Random panning, Caesar encryption is booming...
All the simplest positions can be moved
All the positions are moved
In 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.