【问题标题】:Random numbers in array with no sequential repeats数组中没有连续重复的随机数
【发布时间】:2022-02-25 16:23:40
【问题描述】:

有谁知道如何对数组中的数字进行排序而不重复。

我有可能生成如下示例中的数字数组:

// some function to generate this array of numbers
function genNumbers() {
  // ...code to generate numbers and final return
  return [1,1,8,2,5,9,1,3,3,4] // return this array of numbers
}

生成的数字没问题,但我需要这样的非重复数字:

[1,8,1,2,5,9,1,3,4,3] // all numbers from generated function but in different order.

数组包含更多相同的数字,例如示例中的 13,但不是一个接一个。

谢谢!

【问题讨论】:

  • 如果没有这样的可能顺序怎么办?例如,如果数组是[1, 2, 1, 1]
  • 数一数你有多少个,然后选择并减少计数,转到下一个数字......重复。
  • 如果数组 [1,2,1,1] 仅使用范围(范围从 1 到 10)中的其他数字更新该索引。
  • 那么您为什么不从一开始就应用该策略呢?只需生成第一个数字,将其放入数组中,生成第二个数字,但要求它与第一个数字不同,......等等。

标签: javascript


【解决方案1】:

其中一位评论者建议了一个好方法:随机选择,只是不要选择您选择的最后一个。在大纲级别...

function arrayWithNoRepeat(length) {
  const result = [];
  for (let i=0; i<length; i++) {
    let lastValue = i ? result[i-1] : null;
    result.push(randomButNot(lastValue));
  }
  return result;
}

如何构建randomButNot()?这里有几个选择:

对于较小的范围,构建一组可选择的值并选择一个...

// the range of this function is ints 0-9 (except anInt)
function randomButNot(anInt) {
  const inBoundsValues = [0,1,2,4,5,6,7,8,9].filter(n => n!==anInt);
  const randomIndex = Math.floor(Math.random()*inBoundsValues.length);
  return inBoundsValues[randomIndex];
}

对于大范围,一个想法是在循环中进行选择以防止重复...

// the range is 0-Number.MAX_SAFE_INTEGER (except anInt)
function randomButNot(anInt) {
  const choose = () => Math.floor(Math.random()*Number.MAX_SAFE_INTEGER)
  let choice = choose();
  // we'd have to be very unlucky for this loop to run even once
  while (choice === anInt) choice = choose();
  return choice;
}

关于范围大小、速度、确定性和分布均匀性的其他替代方案的讨论时间更长,但我会把它留给比我更聪明的人。

【讨论】:

    【解决方案2】:

    试试这个,

    function genNumbers() {
        // ...code to generate numbers and final return
        // let random = Array.from({ length: 10 }, () => Math.floor(Math.random() * 40)); //use this to generate random numbers
        let random = [5, 3, 6, 8, 1, 2, 2, 4, 9, 1]; // use this to generate fixed numbers
        let randomSorted = random.sort((a, b) => {
            return a - b;
        }) // sort the numbers
        let filterSame = randomSorted.filter((item, index) => {
            return randomSorted[index + 1] !== item;
        }) // filter the same numbers
    
        return filterSame // return the final result
    }
    
    console.log(genNumbers());

    【讨论】:

      【解决方案3】:

      您可以使用 Set 对象。 Mdn

      let myList = [1,8,1,2,5,9,1,3,4,3]
      
      let unique = [... new Set(myList)]
      
      unique.sort((a,b)=> {return a-b})
      
      console.log(unique)

      【讨论】:

        【解决方案4】:

        其实我就是这样解决我的问题的。 如果current number = previous 重复生成过程,直到得到正确顺序的数组。也许不是解决这个问题的最佳方法,但它足够快。

        这里是函数:

        function genNumbers() {
            let num = [];
            
            const generate = () => {
              for (let x = 0; x < 10; x++) {
                const _number = Math.floor(Math.random() * (10 - 1) + 1); 
                num.push(_number);
        
                // if current number === previous
                if (_number === num[x - 1]) {
                  num = []; // empty array
                  return generate(); // generate again (repeat process)
                }
              }
            };
        
            generate();
        
            return num
          }
          
          console.log(genNumbers())

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-02
          • 1970-01-01
          • 2016-02-16
          • 1970-01-01
          • 2015-02-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多