# How to generate unique random numbers relative to the gap?

Hello, testirovanie!
You must obtain a unique random number in a certain range and put them into an array, but they must be unique in the number and relative gap. In words, it is not clear so here is an example:

I'm calling the function `getRandomArray(10, 1, 100, 5)`, where 10 is the number of integers, 1 is the minimum number, 100 is the maximum number of 5 - "a space of uniqueness", then the function has returned a number like 50 and put it in an array. But this array should not be numbers in the range ((50 - 5) - (50 + 5)), those 45-55. That is, in this array should not be numbers 45, 46, 47, 48, ... , 53, 54, 55 and so on.

Now there is a code which is not working due to the 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;
},
arrayRandom function(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--;
}
}

}

}
}``````

A little explanation to the code:
Function `rangeByInt`returns an array with the numbers that are calculated by subtraction and addition of an 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]``

Function `arrayRandom`takes the following parameters: len - number of digits, min, max - range values for random number generation. But unfortunately, when you add a for loop iterating through range website just hangs and after some time throws an error about lack of memory
March 19th 20 at 09:24
March 19th 20 at 09:26
Solution
If I understand correctly:
``````getRandoms function(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;
}``````
Everything is fine, except that sometimes the numbers just don't appear, for example:
``````const first = this.getRandoms(15, 100, 980 - 200, 50);
const second = this.getRandoms(15, 100, 1750 - 200, 50);``````

In this conclusion is sometimes not displayed first

For example output
70 - 626
... - 1029 // ... - no number
650 - 1321
851 - 1562
740 - 206
143 - 1427 - Michale_Batz58 commented on March 19th 20 at 09:29
@Dora_Cole And under such call options possibleNumbers looks very strange, I do not quite understand what is being done in the first 2 rows - Michale_Batz58 commented on March 19th 20 at 09:32
@ArchiTawer,
getRandoms(15, 100, 980 - 200, 50)

And unless under such conditions and input data in General you can generate 15 numbers? - Dora_Cole commented on March 19th 20 at 09:35
@Dora_Cole, Yes, indeed, thanks for the help - Michale_Batz58 commented on March 19th 20 at 09:38
March 19th 20 at 09:28
Solution
``````function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

getRandomArray function(len, min, max, mid) {
const res = [];
let rand;
for (let i = 0; i < len; i++) {
if (Math.random() < 0.5) {
rand = getRandomInt(min, (max - min) / 2 + min - mid);
}
else {
rand = getRandomInt((max - min) / 2 + min + mid, max);
}
Res. push(rand);
}
return res;
}

getRandomArray(10, 100, 1750, 30);``````

https://repl.it/repls/LiveWorthlessJavabytecode

Find more questions by tags Random numbersMathematicsJavaScript