【问题标题】:Javascript - generating random pairs of numbers if pair doesn't already existJavascript - 如果对不存在,则生成随机数字对
【发布时间】:2013-10-24 22:09:02
【问题描述】:

编辑:如果您阅读 Matt Bryant 的回答,您会发现它应该可以工作,但他使用 indexOf() 方法,并且该方法不适用于 IE 8 或更高版本,我需要它在 IE 8 上工作。我尝试将其作为 indexOf() 方法的变通方法,但它不起作用。

var tester = -1;
for (var test=0; test<xposition.length; test++) {
    if (x == xposition[0]) {
        tseter = x;
    }
}

知道为什么它不起作用吗?

原问题: 所以我想生成随机的数字对,但前提是尚未生成数字对。这是我尝试过的,希望如果您阅读了我尝试过的内容,您会明白我到底需要什么。

function randomPairs() {
    var xposition = []; //array which holds all x coordinates
    var yposition = []; //array which holds all y coordinates
    for (var i=0; i<5; i++) { //do everything below 5 times (generate 5 pairs)

        var x = getRandom(1,7); //generate new x point
        var y = getRandom(2,7); //generate new y point

        if ( jQuery.inArray(x, xposition) ) { //if newly generated x point is already in the xposition array (if it was already previously generated
             var location = xposition.indexOf(x) //find the index of the existing x

             if (y == yposition[location]) { //if the newly generated y points equals the same y point in the same location as x, except in the yposition array

                 while ( y == yposition[location]) {
                     y = getRandom(2, 7); //change y
                 }
             }
       }
  }
  xposition.push(x); //put x into the array
  yposition.push(y); //put y into the array
}

那么,知道为什么它不起作用吗?我是否正确使用了 jQuery.inArray() 和 .indexOf() 方法?

哦,getRandom 是

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

基本上,它会生成一个介于最小值和最大值之间的数字。

另外,当我尝试这样做时

alert(xposition);
alert(yposition);

它是空白的。

【问题讨论】:

  • 你能告诉我们你的getRandom()函数是做什么的吗?为什么 x 和 y 的参数不同?您是否只是传入轴的比例并执行Math.random?只是确保。
  • function randomPairs() { return [4,4]; }。直接来自xkcd.com/221
  • jQuery.inArray(x, xposition) 如果存在则返回元素的索引,那么为什么要在此函数索引中搜索?
  • @aug 哎呀,我刚刚编辑了我的帖子并展示了 getRandom() 的作用。
  • 这是个玩笑老兄...讽刺到需要解释;)

标签: javascript jquery arrays search


【解决方案1】:

问题是您将xy 添加到循环外的数组中。对此的修复(加上删除不需要的 jQuery)是:

function randomPairs() {
    var xposition = []; //array which holds all x coordinates
    var yposition = []; //array which holds all y coordinates
    for (var i=0; i<5; i++) { //do everything below 5 times (generate 5 pairs)

        var x = getRandom(1,7); //generate new x point
        var y = getRandom(2,7); //generate new y point

        var location = xposition.indexOf(x);
        if (location > -1) { //if newly generated x point is already in the xposition array (if it was    already previously generated
            if (y == yposition[location]) { //if the newly generated y points equals the same y point in  the same location as x, except in the yposition array
                while ( y == yposition[location]) {
                    y = getRandom(2, 7); //change y
                }
            }
        }
        xposition.push(x); //put x into the array
        yposition.push(y); //put y into the array
    }
}

请注意,您可能应该从此函数返回一些内容。

如果必须支持旧浏览器,请替换该行

var location = xposition.indexOf(x);

var location = jQuery.inArray(x, xposition);

【讨论】:

  • 呃,'if (location > -1) 有什么作用?如果在数组中找不到 x,xposition.indexOf(x) 是否返回负数?
  • 如果x不在数组中,indexOf(x)返回-1。否则返回一个索引,该索引必须大于等于 0。
  • hm,您是否尝试过您在计算机上提供的代码?它对你有用吗?
  • 出于某种原因,代码在到达“var location = xposition.indexOf(x);”时停止工作
  • 解决方法是使用 jQuery 方法。我会更新的。
【解决方案2】:

这种方法的一个主要问题是,您必须考虑存在多个具有相同 x 或 y 值的唯一对的情况。

x = [1, 1, 1], y = [1, 2, 3]

请注意,Array.indexOf 仅在可以在数组中找到给定元素时返回 first 索引。因此,您必须从找到匹配项的索引开始递归地运行它。

不用 jquery 就可以生成一对唯一整数的简单方法: http://jsfiddle.net/WaFqv/

我假设顺序确实很重要,因此 x = 4, y = 3 &amp; x = 3, y = 4 将被视为两个唯一对。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 2018-05-03
    • 2013-03-13
    • 1970-01-01
    相关资源
    最近更新 更多