在JavaScript中,如何获得一组特定范围内的随机整数,其中没有任何重复?[重复]

vfh0ocws  于 2023-03-21  发布在  Java
关注(0)|答案(3)|浏览(144)

此问题在此处已有答案

(32个答案)
2天前关闭。
有没有办法在一定范围内生成随机数?我正在用React为我的音频播放器做一个随机播放功能。我的song.id运行良好。一旦一个随机数被“使用”,我就不想再使用它了。
但是我想随机生成一个以0开始,以51结束的数字(它可以生成0和51)。我试过这些,但我一直在欺骗:

Math.floor(Math.random() * 52))
xxls0lw8

xxls0lw81#

下面是一种获取播放列表的随机数列表的方法(52个索引的数组的代码相同):

let originalArray=[0, 1, 2, 3, 4];
    let arrayLength = originalArray.length;
    let playlist;
    let rndVal;
    let removed;
    
    function generatePlaylist(){
        playlist = [];
            for( var i = 0; i<arrayLength; i++){
                rndVal = Math.floor(Math.random() * originalArray.length);
                removed = originalArray.splice(rndVal,1);
                playlist.push(removed);
                console.log("value = " + rndVal + " Array = " + originalArray + " removed = " + removed);
            }
        return playlist;
    }
    console.log ("playlist = " + generatePlaylist());

编辑:更可重复使用:

let originalArray=[0, 1, 2, 3, 4];
    let arrayLength;
    let playlist;
    let rndVal;
    let removed;
    
    function generatePlaylist(arrayNum){
        playlist = [];
        arrayLength = arrayNum.length;
            for( var i = 0; i<arrayLength; i++){
                rndVal = Math.floor(Math.random() * arrayNum.length);
                removed = arrayNum.splice(rndVal,1);
                playlist.push(removed);
                console.log("value = " + rndVal + " Array = " + arrayNum + " removed = " + removed);
            }
        return playlist;
    }
    
    console.log ("playlist = " + generatePlaylist(originalArray));
nkkqxpd9

nkkqxpd92#

下面是一个使用数组重排技术的示例。

// shuffle swiped from https://stackoverflow.com/a/2450976
function shuffle(array) {
  let currentIndex = array.length,  randomIndex;

  // While there remain elements to shuffle.
  while (currentIndex != 0) {

    // Pick a remaining element.
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    // And swap it with the current element.
    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }

  return array;
}

// build an array containing 0-51
const arr = [];
for (let i = 0; i < 52; i++) arr.push(i);

shuffle(arr);

// use arr.shift() to get the next song id
console.log("first song is " + arr.shift());
console.log("second song is " + arr.shift());
console.log("third song is " + arr.shift());
0dxa2lsx

0dxa2lsx3#

我最近也在建设这样的功能。

const usedIndexEntries = []; // array for recording utilised indexes

function getRandomIndex() {
  let randomIndex = Math.floor(Math.random() * 52);
  if(usedIndexEntries.length == 52){
    usedIndexEntries = [];
  }
  while (usedIndexEntries.includes(randomIndex)) { // check if the index has already used
    randomIndex = Math.floor(Math.random() * 52); // generate a new random index if it has
  }
  usedIndexEntries.push(randomIndex); // add the index to the array of used indexes
  return randomIndex;
}

这里我正在生成一个随机索引,如果它已经被使用了,那么它将重新生成,直到找到一个唯一的索引,当它达到最大值时,我们将清空数组,这个过程再次开始。

相关问题