How to constantly generate an array with random but close numbers in Javascript?

  • 0
    Task:
    I have an interval function that generates a new array every second with random numbers.
    I need random numbers, but so that they are close in value. Something similar to a processor performance meter. That is, the numbers are constantly jumping, but they do not go far from each other.

    Current result: the numbers are generated quite randomly.
    Desired result: the numbers are generated within a close value

    Something like this:


    What is at the moment:
    function generateNumberArray(min, max, arrayLength) {
          let randoms = Array.from({ length: arrayLength }, () =>
            Math.floor(Math.random() * (max - min + 1) + min)
          );
          return randoms;
        }
    
    const usersChoiceTanks = setInterval(() => {
          socket.emit("usersChoiceTanks", generateNumberArray(10, 80, 8));
        }, 1000);
    JavaScript Anonymous, Feb 20, 2019

  • 1 Answers
  • 0
    There is Perlin Noise (Simplex Noise 2D) returning numbers in the interval [0; 1] , or in the range [-1; 1] , depending on the implementation. If you find one that returns numbers in the [-1; 1] , then turn it into an interval [0; 1] (divide the resulting number by 2 and add 0.5) and multiply by a certain coefficient.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!