【问题标题】:Javascript: randomly select multiple elements from an array (Not a single value selection) [duplicate]Javascript:从数组中随机选择多个元素(不是单个值选择)[重复]
【发布时间】:2020-05-08 03:52:17
【问题描述】:

我正在尝试编写代码以从数组中选择 n 个随机元素。例如:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
    if (n > originalArray.length) {
        return originalArray;
    } else {
        //Some Code to randomly select n elements from orignialArray
    }
}

const newArray = randomSelection(2); //Random selection of 2 elements from the above array

实现此目的的代码是什么?我搜索了案例,但想知道是否有更简单、更直接的方法来做到这一点。

有什么建议吗?提前非常感谢!

【问题讨论】:

标签: javascript arrays


【解决方案1】:

此代码从数组中生成n 随机元素,并避免重复:

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
  let newArr = [];
  if (n >= originalArray.length) {
    return originalArray;
  }
  for (let i = 0; i < n; i++) {
    let newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    while (newArr.includes(newElem)) {
      newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    }
    newArr.push(newElem);
  }
  return newArr;
}

console.log(randomSelection(2));
console.log(randomSelection(5));
.as-console-wrapper { max-height: 100% !important; top: auto; }

如果您不关心重复,请删除while

const originalArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];

const randomSelection = (n) => {
  let newArr = [];
  if (n >= originalArray.length) {
    return originalArray;
  }
  for (let i = 0; i < n; i++) {
    let newElem = originalArray[Math.floor(Math.random() * originalArray.length)];
    newArr.push(newElem);
  }
  return newArr;
}

console.log(randomSelection(2));
console.log(randomSelection(5));
.as-console-wrapper { max-height: 100% !important; top: auto; }

【讨论】:

    【解决方案2】:

    你可以做类似的事情

    let randArr = [];
    while(let i = 0; i < n; i++)
         let chosen = Math.floor(Math.random() *  originalarray.length);
    
         randArr[i] = originalarray[chosen];
    }
    return randArr;
    

    那么你可以从数组中检索多个随机值。

    【讨论】:

      【解决方案3】:

      试试这个:

      const originalArray=["a","b","c","d","e","f","g","h","i"];
      
      
      function RandomElements(n){
        const newArray = []
      
          if (n>originalArray.length) {
              return originalArray;
          }
          else {
            for(let i = 0; i < n; i++){
          newArray.push(originalArray[Math.floor(Math.random() * originalArray.length)])
        }
        return newArray;
          }
      
      }
      
      console.log(RandomElements(2))

      【讨论】:

        【解决方案4】:
        function randArr(n) {
            let ta = arr, i=0;
            while(i<n) {
                let ri = Math.floor(Math.random()*ta.length);
                ta[i] = [ta[ri], ta[ri]=ta[i]][0];
                i++;    
            }
            ta.splice(i, ta.length);
            return ta;
        }
        

        选择随机时,数组元素不会重复。

        【讨论】:

          猜你喜欢
          • 2012-01-23
          • 2015-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-12
          • 2014-07-23
          相关资源
          最近更新 更多