在'min'和'max'之间创建一个随机的非重复数字序列[duplicate]
这个问题在这里已有答案:
- run for random numbers and keep state [duplicate] 4个答案
- Random number, which is not equal to the previous number 16个答案
你可以写一个虚拟的shuffle算法
const arr = [0,1,2,3,4];
const limit = 10;
var i = 0;
while(i++ < limit) {
const a = Math.floor(Math.random() * 5)
const b = Math.floor(Math.random( ) * 5)
if( a !== b){
// swap arr[a] and arr[b]
var temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
基本上对于某些迭代,您可以在数组中交换任意两个随机索引
评论列表(0)
- 暂无评论