0

I am trying to do following:

Create 2 unique numbers that are both within a certain range and they are at least n bigger/smaller.

In example:

Range is 0-600 Minimum "difference" is 150

So the generated numbers could be: [2,400],[120,310],[82,530]

But Not [900,400] or [200,220].

Thats what I have so far:

var posYArray   = [];

for(i=0; i < 2; i++){ 

    var posY    = (Math.random() * 200).toFixed();

    if(i < 1){
        posYArray.push(posY);
    }else{

        for(i=0; i < posYArray.length; i++){ 
            if(posY < posYArray[i]+100){
                posYArray.push(posY);
            }else{
                //Restart loop??
            }
        }

    }

}

But this randomly crashes the browser and also I didnt know a good way to restart the loop when the numbers are too close...

3 Answers 3

3

You could do this in two steps.

  1. Generate your first random number.
  2. Reduce the pool of random numbers which to the only possible valids.
  3. Select your next random number in the reduced pool.

var upperBound = 200,
    minDelta = 90,
    firstRandom = Math.floor(Math.random() * upperBound);

var validPool = [];

for (var i = 0; i < upperBound; i++) {
    if (i < firstRandom - minDelta || i > firstRandom + minDelta) {
        validPool.push(i);
    }
}

var secondRandom = validPool[Math.floor(Math.random() * validPool.length)];

jsFiddle.

It might be slower than randomly choosing and comparing, but at least it has a guaranteed running time :)

Sign up to request clarification or add additional context in comments.

2 Comments

This works just great :) Thanks a lot for your effort to include an example!
this is going to take linear time and linear memory to store, though.
1

You can shift the gap in which not to choose a number, i.e. pick a random number x between 0 and range - gap, than pick first one between 0 and x and the second between x + gap and range. That would be somewhat more efficient.

var range = 600, gap = 150;
var x = Math.floor(Math.random() * (range - gap));
var posX = (Math.random() * (x)).toFixed();
var posY = (Math.random() * (range - x - gap) + x + gap).toFixed();

Works in O(1).

5 Comments

This seems to return huge numbers which are not within the range for the 2nd number... But maybe I made a mistake while applying it to my example.
@Qnan Works just fine now! However I am not sure wheter to use this or alex´s solution, since I am not experienced enough to know what is more efficient.
@Andrej they are doing roughly the same thing, but I would suggest that mine is more efficient and more concise.
It is indeed more concise and I immediatly got what it is doing. I will change the marked answer - I am sure alex wont mind.
@Andrej Indeed this one is much more efficient.
0

What about simply trying them? Very easy and when the difference is a lot smaller then the max size (example 150 and 600) you have a good pair in 1/2 of the possibilities.

For example:

var posY = (Math.random() * 600).toFixed();
var posX = (Math.random() * 600).toFixed();
while(abs(poX-posY) < 150){
    posX = (Math.random() * 600).toFixed()
}

Not that efficient, but when you only have 2 numbers to generate that wont matter !

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.