2
使用Array.prototype.reduce
(或Array.prototype.reduceRight
)的簽名,是否可以從所有索引中以相同概率從數組中選擇一個項目?這裏是我的嘗試:是否可以使用Array.prototype.reduce創建一個線性隨機選擇數組?
document.write(`
${[...'abcdefghijklmnopqrstuvwxyz'].reduce(function(last, next, index, array) {
if (Math.random() > index/array.length) {
return next;
}
return last;
})}
`);
做的這幾個測試運行後,分配似乎對指數較低被扭曲(這是說,上指數往往選擇) 。