Shuffle data simulation Is the following procedure good for shuffling data in general (not just for JavaScript)?



function Shuffle(array) {
  var temp = null,
    len = array.length - 1,
    rnd = null;
  for (var i = len; i &gt; 0; i--) {
    // Generate a random number in which does not exceeds the length of array.
    rnd = Math.floor(Math.random() * len);
    // Assign last item in array to "temp" variable.
    temp = array[len];
    // Assign a random number to last item in array.
    array[len] = array[rnd];
    // Assign value of "temp" variable which may we added previously to the array last random item which has been generated previously. 
    array[rnd] = temp;
  }
  return array;
}

// Implementation
var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
document.getElementById('result').innerHTML = Shuffle(arr);
&lt;div id="result" style="font-size:25px;"&gt;&lt;/div&gt;
Your algorithm is a variant of Fisher-Yates shuffle,
and it's a good algorithm to shuffle a collection.
A more common form is instead of using the last element to swap,
you could have used the decreasing index i to swap,
like this:

  for (var i = len; i &gt; 0; i--) {
    rnd = Math.floor(Math.random() * len);
    temp = array[rnd];
    array[rnd] = array[i];
    array[i] = temp;
  }


It doesn't matter much though, your version is fine too.

Something to improve is a bit cosmetic:
it would be better to name your function with lowercase letters, shuffle,
because it's customary to start with a capital letter only functions that are used to create objects with the new keyword.

Lastly, this is a bit tedious to type:


var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];



You could use this shorter form instead:

var arr = "abcdefghi".split("");