简体   繁体   中英

How to give struct objects random not matching x and y coordinates in C?

I am trying to do my homework, which is some sort of game. This is a part of it and I'm trying to create a function which puts 18 (9 in one team and the other 9 in another) different players on the field. player is a struct which has a name and coordinates. So I tried to write this function and had several problems. I think I have mostly fixed them, but I don't understand what's wrong with it now. Basically this function gives all the players random x and y coordinates, but as I have to make sure that they don't match, I created 2 lists x's and y's. The program takes all the players and add's their x coordinates to x's list if the current player x coordinate matches any x coordinates in x's list, then the program checks the same player's y coordinate and checks if it matches the coordinate of y of the same object in y's list. So if both x and y math, then the program runs again by recursion. The problem I get is that the coordinates I get every time I run the program are same. they don't match but they are not really random cause they don't change when I run them again.

I think I have tried all my knowledge and skills but still can't understand the problem of my code. Can you please tell me what's wrong with this code?

void random_positions()
{
    int i,j;
    int xs[17],ys[17];
    for(i= 0; i<9 ; i++)
    {
     players[i][0].x = rand() % 25;
     players[i][0].y = rand() % 25;
     players[i][1].x = rand() % 25;
     players[i][1].y = rand() % 25;
     printf("A%d x = %d y = %d \n",i+1,players[i][0].x,players[i][0].y);
     printf("B%d x = %d y = %d \n",i+1,players[i][1].x,players[i][1].y);          
    }
    for(i = 0; i < 9 ; i++)
    {
          xs[i] = players[i][0].x; 
          xs[i+8] = players[i][1].x;
          ys[i] = players[i][0].y; 
          ys[i+8] = players[i][1].y;
          for(j = 0; j <= i ; j++)
          {
                //printf("j%d start\n",j);
                if(i != j && xs[i] == xs[j])
                {
                     //printf("i%d start\n",j);
                     if(ys[i] == ys[j])
                     {
                              return random_positions();                                    
                     }
                     //("j%d done\n",j);
                }
                //printf("j%d done\n",j);
          }  
    }
}

A computer is (usually) a deterministic machine; if you run the same program twice, you will get the same answer.

A random number generator generally takes a seed , an initial value that it uses to initialize itself before it starts producing random numbers; give it a different seed, and you will get a different sequence. One way to do this is to give it the current time as a seed:

#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
  /* initialize random seed: */
  srand (time(NULL));

  /* generate random number between 1 and 10: */
  int num = rand() % 10 + 1;

  printf("%d\n", num);

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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