A
A
Artur Karapetyan2019-03-31 15:34:37
JavaScript
Artur Karapetyan, 2019-03-31 15:34:37

How to generate unique random numbers with respect to a span?

Hello toasters!
It is necessary to get unique random numbers in a certain range and put them in an array, but they must be unique, including with respect to the interval. It's not clear at all in words, so I'll give an example:
I call a function getRandomArray(10, 1, 100, 5)where 10 is the number of numbers, 1 is the minimum number, 100 is the maximum number, 5 is the "unique interval", after which the function returned a number, for example, 50 and placed it in array. But this array should not contain numbers in the range ((50 - 5) - (50 + 5)), those are 45-55. That is, this array should not contain the numbers 45, 46, 47, 48, ... , 53, 54, 55, etc.
Now there is a code that does not work due to lack of RAM, although the logic seems to be correct.

function rangeByInt(int, step) {
      var range = [],
        startRange = int - step,
        endRange = int + step;

      for (var i = startRange; i <= endRange; i++) {
        range.push(i);
      }

      return range;
    },
    function arrayRandom(len, min, max) {
      var toReturn = [],
        tempObj = {},
        i = 0;

      for (; i < len; i++) {
        var randomInt = Math.floor(Math.random() * (max - min + min));

        const range = rangeByInt(randomInt, 30);
        for (var j = 0; j < range.length; j++) {
          if ((tempObj["key_" + randomInt] === undefined) & (tempObj["key_" + tempInt[j]] === undefined)) {
            tempObj["key_" + randomInt] = randomInt;
            tempObj["key_" + tempInt[j]] = tempInt[j];
            toReturn.push(randomInt);
          } else {
            i--;
          }
        }
        
      }

      return toReturn;
    }
  }

A little clarification to the code:
The function rangeByIntreturns an array with numbers that are calculated by subtracting and adding from the int parameter step . For example:
console.log(rangeByInt(50, 10)) // [40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60]

The function arrayRandomaccepts the following parameters: len - number of numbers, min, max - range values ​​for generating random numbers. But, unfortunately, when adding a cycle to iterate over range , the site simply hangs and after a while throws an error about the lack of RAM

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Bogachev, 2019-03-31
@architawr

If I understood everything correctly:

function getRandoms(n, min, max, range) {
    let possibleNumbers = Array.from(
        Array(max - min + 1).keys(), x => x + min);
    
    let randoms = [];
    
    for (let i = 0; i < n; i++) {
        if (possibleNumbers.length === 0) {
            break;
        }
        
        const index = Math.floor(Math.random() * possibleNumbers.length);
        const value = possibleNumbers[index];
        
        randoms.push(value);
        
        for (let j = index + range; j > index - range; j--) {
            if (possibleNumbers[j] && Math.abs(possibleNumbers[j] - value) < range) {
                possibleNumbers.splice(j, 1);
            }
        }
    }
    
    return randoms;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question