user9586643
user9586643

Reputation:

Why are the random numbers being generated not in accordance with the conditions in the while loop?

Hi I am implementing an algorithm to create a random maze. In the code below I am choosing a random exit, but i am trying to make the exit be a certain distance from the player. I have written the code below:

do {
r1 = rand() % (size - 1);
r2 = rand() % (size - 1);
} while(r2 - 1 < 0.6*size && r1 - 1 < 0.6*size && mappa[r1][r2] == 1);
mappa[r1][r2] = 3;

In this case i want the exit to be at least 60% the size of the maze. The default position of the players is always 1,1. Now the problem is that when i put for example the size 31 or 21 or some other number the exit with be in the position for example 20,1 in a maze of size 31 which should not happen, where is the problem?

EDIT: The code that generated the maze:

int main(){
 time_t t;
 srand((unsigned) time(&t));

 int **mappa, size;
 printf("Inserire la dimensione della mappa.\n");
 scanf("%d", &size);

 mappa = genera(size); //Genera la mappa
 visualizza(mappa, size); // visualizza la mappa

 return 0;
}

int **genera(int size){
 int **mappa = calloc(size, sizeof(int*)); // Crea la mappa
 for (size_t i = 0; i < size; i++)
  mappa[i] = calloc(size, sizeof(int));

 for (size_t i = 0; i < size; i++) //Riempia la mappa con dei muri
  for (size_t j = 0; j < size; j++)
   mappa[i][j] = 1;

 int *visitata = calloc(size*size, sizeof(int)); // L'array delle visite


 genera_mappa(mappa, size, 1, 1, visitata);

 mappa[1][1] = 2; // posicione di partenza
 int r1; // posicione si arrivo
 int r2;
 do {
  r1 = rand() % (size - 1);
  r2 = rand() % (size - 1);
 } while(r2 - 1 < 0.6*size && r1 - 1 < 0.6*size && mappa[r1][r2] == 1); 
 mappa[r1][r2] = 3;

 return mappa;
}

//Genera il labirinto
void genera_mappa(int **mappa, int size, int x, int y, int *visitata){
 visitata[x*size + y] = 1; // La segna come visitato la posicione attuale.
 mappa[x][y] = 0; //Libera la posicione attuale

 int p[4] = {0}, i = 0;
 if ((x - 2) > 0 && visitata[(x - 2)*size + y] == 0) {
  p[i] = 1;
  i++; }
 if ((y + 2) < size - 1 && visitata[x*size + y + 2] == 0) {
  p[i] = 2;
  i++; }
 if ((x + 2) < size - 1 && visitata[(x + 2)*size + y] == 0) {
  p[i] = 3;
  i++; }
 if ((y - 2) > 0 && visitata[x*size + y - 2] == 0) {
  p[i] = 4;
  i++; }

 if(i != 0){
  int r = rand() % i;
  int r2 = rand() % i;
  switch (p[r]) {
    case 1:
      mappa[x - 1][y] = 0;
      genera_mappa(mappa, size, x - 2, y, visitata);
      break;
    case 2:
      mappa[x][y + 1] = 0;
      genera_mappa(mappa, size, x, y + 2, visitata);
      break;
    case 3:
      mappa[x + 1][y] = 0;
      genera_mappa(mappa, size, x + 2, y, visitata);
      break;
    case 4:
      mappa[x][y - 1] = 0;
      genera_mappa(mappa, size, x, y - 2, visitata);
      break;
  }
  switch (p[r2]) {
    case 1:
      mappa[x - 1][y] = 0;
      genera_mappa(mappa, size, x - 2, y, visitata);
      break;
    case 2:
      mappa[x][y + 1] = 0;
      genera_mappa(mappa, size, x, y + 2, visitata);
      break;
    case 3:
      mappa[x + 1][y] = 0;
      genera_mappa(mappa, size, x + 2, y, visitata);
      break;
    case 4:
      mappa[x][y - 1] = 0;
      genera_mappa(mappa, size, x, y - 2, visitata);
      break;
  }
 }
}

Upvotes: 0

Views: 80

Answers (1)

Neil
Neil

Reputation: 1922

One is randomly generating random numbers until one of these negated conditions is met,

r2 - 1 < 0.6*size && r1 - 1 < 0.6*size && mappa[r1][r2] == 1

One probably wants all of the negated conditions to be met. By De Morgan's law,

r2 - 1 < 0.6 * size || r1 - 1 < 0.6 * size || mappa[r1][r2] == 1

Or just generate a number that's within 0.6 * size straight off, for pseudo-uniform range between [M, N],

M + rand() / (RAND_MAX / (N - M + 1) + 1)

Upvotes: 2

Related Questions