Knuth shuffle: Difference between revisions
Content added Content deleted
m (→ES5) |
|||
Line 2,646: | Line 2,646: | ||
for (i = arr.length - 1; i > 0; i -= 1) { |
for (i = arr.length - 1; i > 0; i -= 1) { |
||
rand = Math.floor((i + 1) * Math.random());//get random between zero and i (inclusive) |
rand = Math.floor((i + 1) * Math.random());//get random between zero and i (inclusive) |
||
temp = arr[rand]; |
temp = arr[rand]; |
||
arr[rand] = arr[i]; |
arr[rand] = arr[i]; //swap i (last element) with random element. |
||
arr[i] = temp; |
arr[i] = temp; |
||
} |
} |
||
Line 2,673: | Line 2,673: | ||
3,1,2 16460 |
3,1,2 16460 |
||
3,2,1 16596</pre> |
3,2,1 16596</pre> |
||
===ES6=== |
===ES6=== |