임의의 요소를 표시하도록 JavaScript 배열을 임의화하려면 다음 코드를 실행해 보십시오.
예
<html>
<body>
<script>
function randomFunc(myArr) {
var l = myArr.length, temp, index;
while (l > 0) {
index = Math.floor(Math.random() * l);
l--;
temp = myArr[l];
myArr[l] = myArr[index];
myArr[index] = temp;
}
return myArr;
}
var arr = [10, 20, 30, 40, 50];
document.write(randomFunc(arr));
</script>
</body>
</html> 출력
20,50,40,30,10