【问题标题】:How can I generate an array of random numbers that fluctuate by 1 in javascript?如何在javascript中生成一个波动为1的随机数数组?
【发布时间】:2021-03-11 15:19:37
【问题描述】:

我希望能够在 javascript 中生成以下数组(或类似的东西):

[
  52, // random number 0-100
  53, // random +1 or -1
  54, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  53, // random +1 or -1
  52, // random +1 or -1
  51, // random +1 or -1
  50, // random +1 or -1
  51, // random +1 or -1
  // etc., etc., etc.
]

我该怎么做?

我试过这个,但我总是得到一个随机数,后跟 1 和 -1:

Array(50).fill(0).map((v, i, a) => i !== 0 ? (Math.round(Math.random()) ? a[i-1] + 1 : a[i-1] - 1) : Math.floor(Math.random() * 101))

【问题讨论】:

  • 是前一个值的±1吗?还是初始随机生成值的 ±1?
  • 问题不清楚。你到底想做什么?
  • @ItoPizarro 是之前的值,不过array[0]是随机生成的
  • @odedbartov 我正在尝试生成一个数组,从 0 - 100 的随机数开始,例如 50,然后是 50 + 1 (51) 或 50 - 1 (49) (随机选择)。对于下一个,如果选择 49,则后面是 48 (49 - 1) 或 50 (49 + 1),随机选择。
  • 我发布了一个答案,看看吧

标签: javascript arrays random


【解决方案1】:

这可能会有所帮助:

function randomGenerator(size) {
    let result = [];
    let firstValue = Math.round(Math.random() * 100);
    result.push(firstValue);
    
    for (let i=0;i<size-1;i++){
        firstValue += Math.random() > 0.5 ? 1:-1;
        result.push(firstValue)
    }
    return result;
}

console.log(randomGenerator(10));
console.log(randomGenerator(13));

或者,如果您更喜欢使用功能,您可以利用默认值 和comma operator

const randGenerator = (size = 13,init = Math.round(Math.random() *100)) => 
Array(size).fill(0).map(e => (init += Math.random() > 0.5 ? 1:-1,init))

console.log(randGenerator(10))
console.log(randGenerator(13))

【讨论】:

    【解决方案2】:

    简单的解决方案:

    function rand(min, max) {
      return min + Math.floor((max + 1 - min) * Math.random())
    }
    
    function generate(size) {
      let last = rand(0, 100)
      const array = []
      for (let i = 0; i < size; i++) {
        array.push(last)
        last += rand(0, 1) ? 1 : - 1
      }
      return array
    }
    
    console.dir(generate(50))

    【讨论】:

      【解决方案3】:

      这里是如何使用递归函数来实现的,你可以设置随机数的初始最小值和最大值,还可以指定最终的数组长度:

      const arr = [];
      
      function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1) + min);
      }
      
      function updateArr(min, max, arrLength, arr) {
        if (arrLength) {
          let resultNum = getRandomInt(min, max);
          if (resultNum !== arr[arr.length - 1]) {
            arr.push(resultNum);
            updateArr(resultNum - 1, resultNum + 1, arrLength - 1, arr);
          } else updateArr(min, max, arrLength, arr);
        }
      }
      
      updateArr(0, 100, 10, arr);
      
      console.log(arr);

      【讨论】:

        【解决方案4】:
        arr = [Math.random()*100]
        for (let i = 0; i < 49; i++){
        arr.push(arr[i] + (Math.random() > 0.5? 1 : -1))
        }
        

        这对你有用吗?

        【讨论】:

          猜你喜欢
          • 2016-01-04
          • 2016-03-27
          • 2011-07-30
          • 1970-01-01
          • 1970-01-01
          • 2014-06-30
          • 1970-01-01
          • 2019-11-20
          • 1970-01-01
          相关资源
          最近更新 更多