export default function App() {
Array.prototype.shuffle = function () {
let rnum=[]
let random = this
for (let i= 0; i < random.length; i++){
let randomizer = Math.floor(Math.random()*(this.length));
rnum.push(randomizer)
}
console.log(rnum)
}
let random = [0,1,2,3,4].shuffle()这是我当前的代码。它目前打印一个随机数组的“随机”数组,无论我如何尝试打印所有可用的数字,而没有任何一个数字在"rnum“数组中重复。如果有人能帮我,我将不胜感激!
发布于 2021-10-05 13:39:56
let index= this.length, randomIndex
while(index != 0) {
randomIndex = Math.floor(Math.random()*index)
index--
[this[index], this[randomIndex]] = [this[randomIndex], this[index]]
}
return this
}
var random = [0,1,2,3,4].shuffle()
console.log(random) 我使用Samuel Goldenbaum的建议解决了这个问题,查看了JS“如何随机化(随机处理)一个JavaScript数组?”中的前一个解决方案。我只是让你把所有的"array“替换成"this"!
https://stackoverflow.com/questions/69440380
复制相似问题