с повторами
Array.prototype.random = function(n) {
var l = this.length,
newArr = [] ;
while(n--)
newArr.push(this[Math.floor(Math.random() * l)]) ;
return newArr ;
} ;
var arr = [1, 2, 3, 4, 5] ;
alert(arr.random(3)) ;
Без повторов
Array.prototype.random = function(n) {
var oldArr = this,
newArr = [] ;
while(n--) {
newArr.push(oldArr.splice(Math.floor(Math.random() * oldArr.length), 1)[0]) ;
} ;
return newArr ;
} ;
var arr = [1, 2, 3, 4, 5] ;
alert(arr.random(3)) ;