Shuffler //JavaScript Repository
Description
Scrambles the elements of an array.
Created: 2005.11.03
Created: 2005.11.03
Code (Download)
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [rev. #1]
shuffle = function(v){
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
return v;
};
Example (Example)
<script type="text/javascript">
//<![CDATA[
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write("A = ", a.join(","), "<br />shuffle(A) = ", shuffle(a));
//]]>
</script>
Help
- shuffle(vector: Array): Array
-
Returns an array with the values scrambled.
- vector
- array that will be scrambled
Rank (Votes: 544)
3.24