Skip to content

Instantly share code, notes, and snippets.

View sample.js
Array.prototype.sample = function(n) {
return Array.from(this).sort(_ => Math.random() - 0.5).splice(0, n > this.length ? this.length : n||1)
}
let hoge = [1,2,3,4,5];
console.log(hoge.sample(3));
console.log(hoge.sample(6));
You can’t perform that action at this time.