var arr1 = new Array(); var arr2 = new Array(); for(var i = 0; i<20; i++){ arr1.push(i); } for(var k = 0; k<10;k++){ var id = Math.ceil(Math.random()*19); if(arr2.indexOf(arr1[id]) === -1){ arr2.push(arr1[id]); }else{ k= k - 1; continue; } } console.log(arr2);