You could shuffle the array and pick the first four.
numbers.sort( function() { return 0.5 - Math.random() } );
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…