简体   繁体   English

在C语言中选择二维数组的非重复随机元素

[英]Selecting Non-Repeating Random Elements of 2 Dimensional Array in C

I'm pretty new to programming, so the solution needs to be simple. 我是编程新手,所以解决方案必须简单。 What I need to do is generate an 8x8 matrix, and, because of what I'll be doing with it later, I need to set all the elements equal to 0. Then, I need to select 20 of the elements randomly (without picking the same one twice) and change those elements to 1. With what I have now, I usually print 15 to 18 "1's" every time. 我需要做的是生成一个8x8的矩阵,并且由于稍后要处理,所以我需要将所有元素设置为0。然后,我需要随机选择20个元素(不进行选择)两次),然后将这些元素更改为1。使用我现在拥有的内容,通常每次每次打印15到18个“ 1”。 Meaning 2 to 5 repeats. 表示2到5次重复。 That in itself seems weirdly consistent to me, so I suspect there must be something bigger that I'm missing. 这本身对我来说似乎很奇怪,所以我怀疑肯定有更大的缺失。 I've seen other posts similar talking about randomizing possible elements and then selecting from that list, but the code needed to do that is a little over my head at this point. 我看到过其他类似的文章,它们讨论了将可能的元素随机化,然后从该列表中进行选择,但此时所需的代码有点麻烦。

Is there something flawed with my approach here? 我的方法有什么缺陷吗?

int Board[8][8];
    int x, y, i, a, b;
    for (x = 0; x < 8; x++)
    {
        for (y = 0; y < 8; y++)
            Board[x][y] = 0;     //配列の定義
    }
        srand(time(NULL));     //任意なマスを1に設定
        for (i = 0; i < 20; i++)
        { 
                a = rand() % 8;
                b = rand() % 8;

                Board[a][b] = 1;
        }
    //盤面の出力
    for (x = 0; x < 8; x++)
    {
        for (y = 0; y < 8; y++)
            printf("  %d ", Board[x][y]);
        printf("\n");
    }

You could insert a do-while loop repeating the random number generation in case an existing pair is generated. 如果生成现有对,您可以插入一个do-while循环来重复随机数的生成。 Such as: 如:

int Board[8][8];
int x, y, i, a = 0, b = 0;
for (x = 0; x < 8; x++)
{
    for (y = 0; y < 8; y++)
        Board[x][y] = 0;     //配列の定義
}
    srand(time(NULL));     //任意なマスを1に設定
    for (i = 0; i < 20; i++)
    { 
            /* Repeat until found a '0'-valued cell*/
            do {
                a = rand() % 8;
                b = rand() % 8;
            } while(Board[a][b] == 1);

            Board[a][b] = 1;
    }
//盤面の出力
for (x = 0; x < 8; x++)
{
    for (y = 0; y < 8; y++)
        printf("  %d ", Board[x][y]);
    printf("\n");
}

rand()%8 will always return values from 0-7 in both of your lines for a and b as you may be knowing. rand()%8将始终在ab两行中返回0-7之间的值。 So it may happen that it returns like (6,6) twice. 因此可能会发生两次返回(6,6)的情况。 The number of repetitions scaling upto five is a bit weird really when you are iterating over only 20 times. 当您仅迭代20次时,重复次数最多扩展为5确实有点奇怪。 But only twice shows you are doing good too. 但是只有两次表明您也表现良好。

You can try this 你可以试试这个

a=rand()%8; b=(a+(i%8))%8;

Or if you want to be pitch-perfect, make a list of pairs of ( a , b ) and every time your original code generates a pair, you match it with the list to see if it is unique and then only you add it to the list and increment your loop counter. 或者,如果您想获得完美的音调,则列出( ab )对,然后每次您的原始代码生成一对时,都将其与列表匹配以查看其是否唯一,然后仅将其添加到列表并增加循环计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM