生成唯一行、列的随机列表 javascript

发布于 2024-10-19 13:32:07 字数 175 浏览 2 评论 0原文

我将如何在 javascript 中生成(并单独打印)唯一列、行对的列表? 我设置了两个变量,一些列和一些行。我只希望每对出现一次,并且其中不能有 0。假设我有 3 行和 3 列,我想要:

1,2
3,1
2,3
1,3
1,1
2,1
3,2
2,2
3,3

全部按随机顺序排列。我该怎么做?

How would I generate (and print individually) a list of unique column,row pairs in javascript?
I have two variables set, a number of columns and a number of rows. I only want each pair to appear once, and it can't have 0s in it. Say I had 3 rows and 3 columns, I would want:

1,2
3,1
2,3
1,3
1,1
2,1
3,2
2,2
3,3

All in a random order. How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巾帼英雄 2024-10-26 13:32:07

虽然不是我的算法

<script type="text/javascript">

        var array = Array();

        var i,j;

        for(i=1; i <= 3; i++){
            for(j=1; j<=3; j++){    
                array.push(j+', '+i);
            }
        }
        var newarr = shuffle(array);
        console.log(newarr);
        document.write(newarr);

    function shuffle(array)
    { //v1.0
        for(var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x);
        return array;
    };
</script>

Not my algorithm though

<script type="text/javascript">

        var array = Array();

        var i,j;

        for(i=1; i <= 3; i++){
            for(j=1; j<=3; j++){    
                array.push(j+', '+i);
            }
        }
        var newarr = shuffle(array);
        console.log(newarr);
        document.write(newarr);

    function shuffle(array)
    { //v1.0
        for(var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x);
        return array;
    };
</script>
歌入人心 2024-10-26 13:32:07

您应该生成所有可能的坐标对的数组,使用洗牌算法来放置将它们按随机顺序排列,然后打印出来。

You should generate an array of all of the possible coordinate pairs, use a shuffling algorithm to put them into a random order, then print them out.

陈年往事 2024-10-26 13:32:07

这是一种方法:

var answer = (function(width,height) {
  var result = [];
  for (var i = 1; i <= width; i++) {
    for (var j = 1; j <= height; j++) {
      result.push([i, j]);
    }
  }
  return result.sort(function(a, b) {
    return 0.5 - Math.random();
  });
}(3,3)); // enter width/height here, 1-indexed

编辑:忘记“打印”要求:

for( var k = 0, len = answer.length; k < len; k++ ){
  console.log( answer[k] ); // or your preferred definition of "print"
}

Here's one way:

var answer = (function(width,height) {
  var result = [];
  for (var i = 1; i <= width; i++) {
    for (var j = 1; j <= height; j++) {
      result.push([i, j]);
    }
  }
  return result.sort(function(a, b) {
    return 0.5 - Math.random();
  });
}(3,3)); // enter width/height here, 1-indexed

Edit: Forgot the "print" requirement:

for( var k = 0, len = answer.length; k < len; k++ ){
  console.log( answer[k] ); // or your preferred definition of "print"
}
一片旧的回忆 2024-10-26 13:32:07
Array.prototype.shuffle= function(force){
    var i, temp, L= this.length,
    A= force? this: this.concat();
    while(--L){
        i= Math.floor(Math.random()*L);
        temp= A[i];
        A[i]= A[L];
        A[L]= temp;
    }
    return A;
}

(可选)参数的目的是对数组本身进行洗牌。
默认情况下,数组不会被打乱,但会返回打乱后的副本。

Array.prototype.shuffle= function(force){
    var i, temp, L= this.length,
    A= force? this: this.concat();
    while(--L){
        i= Math.floor(Math.random()*L);
        temp= A[i];
        A[i]= A[L];
        A[L]= temp;
    }
    return A;
}

The purpose of the (optional) parameter is to shuffle the array itself.
By default the array is not shuffled, but a shuffled copy is returned.

番薯 2024-10-26 13:32:07

这应该可行:

// New Array
a = [];
// 3 rows, 3 columns
c = 3;
r = 3;
// fill array with every unique possibility
for( var i = 1; i <= r; i++ ) {
  for( var j = 1; j <= c; j++ ) {
    a[a.length] = "" + i + "," + j;
  }
}
// random pick into another array
result = [];
a_len = a.length;
for( var i = 0; i < a_len; i++ ) {
  result[result.length] = a.splice(Math.floor(Math.random()*a.length), 1)[0];
}

如果您只想打印结果而不是将它们放在数组中,只需执行 print 而不是“result[result.length]”。

This should work:

// New Array
a = [];
// 3 rows, 3 columns
c = 3;
r = 3;
// fill array with every unique possibility
for( var i = 1; i <= r; i++ ) {
  for( var j = 1; j <= c; j++ ) {
    a[a.length] = "" + i + "," + j;
  }
}
// random pick into another array
result = [];
a_len = a.length;
for( var i = 0; i < a_len; i++ ) {
  result[result.length] = a.splice(Math.floor(Math.random()*a.length), 1)[0];
}

If you just want to print the results instead of having them in an array just do print instead of "result[result.length]".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文