C 生成8种不同的协调

C 生成8种不同的协调,c,random,srand,C,Random,Srand,我试着写一个函数,得到一个数组,生成8个不同的坐标。 我有一些大小为8的结构数组,它在另一个数组([8][8])中保存了一些坐标。 我试图得到八个不同的坐标,但不幸的是,有时我得到的坐标是相同的 代码如下: struct location { int x; int y; }; void setCoordinations(struct locaction loc[]) /// the array is empty. { int i = 0, j = 8; srand(time(N

我试着写一个函数,得到一个数组,生成8个不同的坐标。 我有一些大小为8的结构数组,它在另一个数组([8][8])中保存了一些坐标。 我试图得到八个不同的坐标,但不幸的是,有时我得到的坐标是相同的

代码如下:

struct location {
int x;
int y;
};
void setCoordinations(struct locaction loc[]) /// the array is empty.
{
    int i = 0, j = 8; 
    srand(time(NULL));


    array[i].x = rand() % 8;
    array[i].y = rand() % 8;
    for (int i = 1; i <8; i++)
    {
        
        array[i].x = (rand() +i) % 8;
        array[i].y= (rand() -j)  % 8;
    }
结构位置{
int x;
int-y;
};
void setCoordinations(struct locaction loc[])///数组为空。
{
int i=0,j=8;
srand(时间(空));
数组[i].x=rand()%8;
数组[i].y=rand()%8;
对于(int i=1;iIf算法(唯一坐标),这里有一个问题,请检查新创建的坐标与数组中已创建的坐标

void setCoordinations(struct locaction loc[]) /// the array is empty.
{
    int i, j = 8, k; 
    srand(time(NULL));

    loc[0].x = rand() % 8;
    loc[0].y = rand() % 8;
    for (i = 1; i < 8; )
    {
        loc[i].x = (rand() + i) % 8;
        loc[i].y = (rand() - j) % 8;
        for (k = 0; k < i; k++)         // check in already created coordinates
            if(loc[i].x == loc[k].x && loc[i].y == loc[k].y)
                break;
        if(k == i)                      // no match found
            i++;                        // create next coordinates[i]
                                        //   otherwise create coordinates again
    }
void setCoordinations(struct locaction loc[])///数组为空。
{
int i,j=8,k;
srand(时间(空));
loc[0].x=rand()%8;
loc[0]。y=rand()%8;
对于(i=1;i<8;)
{
loc[i].x=(rand()+i)%8;
loc[i].y=(rand()-j)%8;
对于(k=0;k
如果算法(唯一坐标)出现问题,请使用数组中已创建的坐标检查新创建的坐标

void setCoordinations(struct locaction loc[]) /// the array is empty.
{
    int i, j = 8, k; 
    srand(time(NULL));

    loc[0].x = rand() % 8;
    loc[0].y = rand() % 8;
    for (i = 1; i < 8; )
    {
        loc[i].x = (rand() + i) % 8;
        loc[i].y = (rand() - j) % 8;
        for (k = 0; k < i; k++)         // check in already created coordinates
            if(loc[i].x == loc[k].x && loc[i].y == loc[k].y)
                break;
        if(k == i)                      // no match found
            i++;                        // create next coordinates[i]
                                        //   otherwise create coordinates again
    }
void setCoordinations(struct locaction loc[])///数组为空。
{
int i,j=8,k;
srand(时间(空));
loc[0].x=rand()%8;
loc[0]。y=rand()%8;
对于(i=1;i<8;)
{
loc[i].x=(rand()+i)%8;
loc[i].y=(rand()-j)%8;
对于(k=0;k
初始化每一行/col
0-7
然后再
shuffle
每一行。看这是否回答了你的问题?初始化每一行/col
0-7
然后再
shuffle
每一行。看这是否回答了你的问题?就像有无限次迭代:/。现在我得到了至少2次相同的协调。对不起,我是t从原始帖子复制打字错误(函数和循环中的变量名相同)。代码已更新。就像有无限次迭代:/。现在我得到了至少2次相同的协调。对不起,我只是从原始帖子复制打字错误(函数和循环中的变量名相同)。代码已更新。