最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在'min'和'max'之间创建一个随机的非重复数字序列[duplicate]

IT培训 admin 5浏览 0评论

在'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;
   }
}

基本上对于某些迭代,您可以在数组中交换任意两个随机索引

在'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)

  1. 暂无评论