Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.22 KB | None | 0 0
  1. Array.prototype.sample = function(n) {
  2. return Array.from(this).sort(_ => Math.random() - 0.5).splice(0, n > this.length ? this.length : n||1)
  3. }
  4.  
  5. let hoge = [1,2,3,4,5];
  6. console.log(hoge.sample(3));
  7. console.log(hoge.sample(6));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement