Martin
Martin

Reputation: 47

How can I use two random numbers generated in a function as matrix coordinates?

I came up with this function to generate two random numbers, r and c, so that I can use them in as coordinates in matrix board[r][c]. Is this even possible?

int coordAleatoria()
{
    srand((unsigned int)time(0));
    int r=rand()%9;
    int c=rand()%9;
    while(r==c)
    {
        c=rand()%9;
    }
    printf("%d %d", r, c);
    return r;
    return c;
}

This is for a chess-like game. The PC is supposed to generate random moves. This function does generate coordinates, I'm just not sure how to make my program treat them as coordinates.

I hope I can get r and c in board[r][c] to be the values generated in coordAleatoria().

Upvotes: 1

Views: 194

Answers (3)

chux
chux

Reputation: 153358

  • rand()%9 generates 9 different values. With while(r==c), looks like code is looking for 9*(9-1) or 72 different pairs. For a speedier approach, call rand() once.

  • Code could return a single int and later divine/mod by 9 to recover the row/column.

  • srand((unsigned int)time(0)); should not be repeated called in coordAleatoria(). Call it once, perhaps in main().


int coordAleatoria(void) {
  int rc = rand()%72;
  int r = rc/9;
  int c = rc%9;
  if (r==c) r++;
  return (r*9) + c;
}

Upvotes: 1

hafiz031
hafiz031

Reputation: 2670

You cannot return more than one time. So you can combine the coordinates using structure as Jabberwocky suggested in the comment. If you are still finding it difficult than here is the implementation.

#include<stdio.h>
#include<stdlib.h>//for rand()
#include<time.h>//for time()
struct Pair
{
    int row,col;
};
struct Pair coordAleatoria()
{
    int r=rand()%9;
    int c=rand()%9;
    while(r==c)
    {
        c=rand()%9;
    }
    printf("Inside function: row=%d and col=%d\n",r,c);
    //Create a pair
    struct Pair p;
    //Assign values
    p.row=r,p.col=c;
    //return it
    return p;
}
int main()
{
    srand((unsigned int)time(0));
    //Get the returned value as a Pair
    struct Pair p=coordAleatoria();
    //Collect the row and column values
    int r=p.row;
    int c=p.col;
    //Now you can use them here
    printf("Outside function: row=%d and col=%d\n",r,c);
}

Upvotes: 2

David C. Rankin
David C. Rankin

Reputation: 84551

Rather than calling rand() twice (after properly seeding the random number generator with a call to srand()), you can simply call rand() once and take the first two digits as your coordinates, e.g.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    int x, y;
} pair_t;

void rand_coords (pair_t *coords)
{
    int n = rand();

    coords->x = n % 10;
    n /= 10;
    coords->y = n % 10;
}

int main (void) {

    pair_t coords = { .x = 0 };

    srand (time (NULL));         /* seed random number generator */

    rand_coords (&coords);

    printf ("coords.x : %d\ncoords.y : %d\n", coords.x, coords.y);

    return 0;
}

(or take the modulo of whatever your actual coordinate range is)

Example Use/Output

$ ./bin/coords_rand
coords.x : 9
coords.y : 8

$ ./bin/coords_rand
coords.x : 1
coords.y : 1

$ ./bin/coords_rand
coords.x : 5
coords.y : 7

$ ./bin/coords_rand
coords.x : 8
coords.y : 0

Upvotes: 0

Related Questions