Javascript - Generating Random numbers in a Range,

2020-05-09 09:21发布

问题:

Basically I am creating a grid and plotting points on it, and no two points can be on the exact same place [(3,4) is different than (4,3)]. The y coordinates have to be within 2 and 7 (so 2, 3, 4, 5, 6, 7) and x coordinates have to be within 1 and 7. I have a getRandom function (which can be seen below) which generates a random number between a min and max range. Here is what I have so far.

var xposition = [];
var yposition = [];
var yShouldBeDifferentThan = []

function placeRandom() {
    for (s=0; s<xposition.length ; s++ ) {
        if (xposition[s] == x) { // loops through all numbers in xposition and sees if the generated x is similar to an existing x
             yShouldBeDifferentThan.push(yposition[s]); //puts the corresponding y coordinate into an array.
             for (r=0; r<yShouldBeDifferentThan.length; r++) {
                 while (y == yShouldBeDifferentThan[r]) {
                     y = getRandom(2,7);
                 }
             }
        }
    }
    xposition.push(x);
    yposition.push(y);
}

The problem with this is, if

xposition = [1, 5, 5, 7, 5, 5]
yposition = [1, 3, 7, 2, 3, 6]
yShouldBeDifferentThan = [3, 7, 3, 6]

First, it will generate a random number different thah 3, say 6. Then (I think) it will see: 6 == 7? It doesn't. 6 == 3? It doesn't. 6 == 6? It does, so generate a random number different than 6. This is where the problem comes, it might generate the number 3. My getRandom function is the following:

function getRandom(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));
}

I was thinking making the getRandom function such that I can exclude numbers as well if I want, but I don't know how to do this. If I can get it to exclude numbers, than in the last while loop of the placeRandom function, maybe I can do something like:

y = getRandom(2,7) // excluding all numbers which already exist in the ShouldBeDifferentThan array

Also, note that I cannot use the indexOf method since I am using Internet Explorer 8.

回答1:

There are two problems with your approach:

  • You might pick an x coordinate for a row that is already full, which would send the code into an eternal loop.

  • Picking an x coordinate and then a y coordinate means that positions will have a different chance to be picked depending on how many positions were picked in the same row before.

Instead just pick an x and y coordinate, and check if that specific coordinate was picked before. If it was, start over.

function placeRandom() {
  do {
    var x = getRandom(2,7), y = getRandom(2,7), found = false;
    for (s = 0; s<xposition.length; s++) {
      if (xposition[s] == x && yposition[s] == y) {
        found = true;
        break;
      }
    }
  } while(found);
  xposition.push(x);
  yposition.push(y);
}

Additionaly, when the grid starts to get full (e.g. around 80%), you can make an array containing all the remaining positions and pick one by random from that.



回答2:

var numbers = [ 1, 2, 3, 4, 5 ];
var exclude = [ 3, 4 ];
var filtered = [];
for (var i = 0; i < numbers.length; i += 1) {
    if (exclude.indexOf(numbers[i]) === -1) {
        filtered.push(numbers[i]);
    }
}
var rand = Math.floor(Math.random() * filtered.length);
var num = filtered[rand]; // 1, 2 or 5

Build the list of allowed numbers, pick one of those at random. The for-loop is just a diff between numbers and exclude, like: var filtered = numbers.diff(exclude);