使用Javascript - 在范围生成随机数,不包括某些电话号码 [英] Javascript - Generating Random numbers in a Range, excluding certain numbers

查看:127
本文介绍了使用Javascript - 在范围生成随机数,不包括某些电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我创建一个网格并在其上绘制点,并且没有两个点可以是对相同的位置[(3,4)比不同(4,3)]。的y坐标都必须在2和7(SO 2,3,4,5,6,7)和x坐标必须在1和7我有一个getRandom功能(其在下面可以看到),它生成一个一个最小值和最大值之间的范围内的随机数。这里是我到目前为止所。

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]

首先,它会生成一个随机数不同thah 3,说6.然后(我觉得)它会看到: 6 == 7 ?它没有。 6 == 3 ?它没有。的 6 == 6 ?它,所以产生比6。这是哪里出了问题来了,它可能会产生数量3.我的不同的随机数 getRandom 函数如下:

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));
}

我想制作 getRandom 函数,这样我可以排除号码以及如果我想,但我不知道如何做到这一点。如果我能得到它排除数字,比上一次,而 placeRandom 函数的循环,也许我可以做这样的事情:

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

另外请注意,我不能使用的indexOf 方法,因为我使用的Internet Explorer 8。

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

推荐答案

有两个问题你的方法:


  • 您可能会选择一个x坐标为一行已经满了,这将发送code到一个死循环。

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

采摘的x坐标,然后一个y坐标位置都会有不同的机会,这取决于有多少职位同一行前被挑选被拾起的手段。

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.

而不是只挑选一个X和Y坐标,并检查是否具体坐标之前采摘。如果是,重新来过。

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,当电网开始得到充分的(如80%左右),可以使包含所有其余位置的数组,并选择一个随机从。

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.

这篇关于使用Javascript - 在范围生成随机数,不包括某些电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆