If you want them to appear in a random order, for whatever reason:
<script type="text/javascript">
<!--
var imgCount = 5;
var image = new Array();
for(var i = 0; i < imgCount; i++) {
image.push('image' + (i + 1) + '.gif');
}
while(image.length > 0){
var randomIndex = Math.floor(Math.random() * image.length);
document.write('<img src="' + image[randomIndex] + '" alt="' + image[randomIndex] + '">');
image.splice(randomIndex, 1);
}
//-->
</script> The issue with your code is that when you splice the array you're not taking into account the fact that the array size is changing dynamically. Therefore, you're generating non-existent indices. Whenever you splice an array you're going to be shortening its length, and the result will have new indices. Thus, when you have 5 elements your random number generation needs to be from 0 to 4, when you remove one and have 4 elements it needs to be from 0 to 3, and so on, until you have no more elements left in the array.