Possible Duplicate:
JavaScript: Getting random value from an array
var numbers = new Array('1','2','4','5','6','7','8','9','10');
I have a JavaScript Array and now want to randomly choose four different numbers from it and then express it on the page (through document.write). Obviously each time the page is reloaded by the user it would show four different random numbers.
You could shuffle the array and pick the first four.
Now
numbers[0],numbers[1]… and so on have random and unique elements.Note that this method may not be an optimal way to shuffle an array: see Is it correct to use JavaScript Array.sort() method for shuffling? for discussion.