generating random points in a grid

This is a discussion on generating random points in a grid within the C Programming forums, part of the General Programming Boards category; I have created a grid wich allows the user to type in a width and height and then prints to ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    generating random points in a grid

    I have created a grid wich allows the user to type in a width and height and then prints to the screen with that width and height as dots. Now i need to randomly generate points in the grid to come out as "x" instead of dots. The tricky part is that the user also types in a value wich corresponds to the number of points to be randomly generated into the grid. So say for example he chooses 5, then 5 random dots in the grid will be printed as x's and the rest of the grid will be dots. I am not sure how to go about this and need some help. I have figured out how to randomly generate numbers (inefficiently though). The code for both the grid and random point generation are as follows:

    Code:
    # include <stdio.h>
    # include <math.h>
    # include <stdlib.h>
    
    int lx;
    int ly;
    char grid [10] [10];
    int z;
    int j;
    
    int main()
    
    {
    
    printf ("Insert the width (Lx) and height (Ly) of the grid \n");
    scanf ("%d %d", &lx, &ly);
    
    for (z = 0; z < lx; z++)
    
      { 
       for (j=0; j < ly; j++)
    
        {
          grid [z] [j] = '.';
    
        }
       }
    
    for (z = 0; z < lx; z++)
    
      {
       for (j=0; j < ly; j++)
      
        {
         
         printf ("%c", grid [z] [j]);
         
        }
        printf ("\n");
       }
    
    }


    Code:
    # include <stdio.h>
    main() 
    {
    int random_i(int max);
    int i;
    int nmax=10, nn=20;
    int seed=3;
    
    srandom(seed);
    for (i=0;i<nn;i++) {
    printf("%d\n",random_i(nmax));
    }
    }
    
    int random_i(int max) {
    
    return (random() % max);
    }
    My main problem is that first i have no idea how to go about involving the random numbers in the same code as the grid. and second how would i go about making a random distribution of x's in the grid?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    ask for x
    ask for y
    ask for z (the number of dots to make)
    loop from z times
        grid[ rand() % x ][ rand() % y ] = 'x';
    You may want to read the FAQ on making random numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Given the size of the array x by y, and the number of X's n, you would call a function to generate n random numbers from 1 to x*y and store each in an array passed in as an argument. In the interest of keeping things simple, you can keep the range the same for each random number generated, and just try again if you repeated a prior value.

    Once that function comes back and you have your array of random numbers, you can use that to decide whether or not to put an 'x' in a cell.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Ok thank you both for the insight. Problem is im very new to c programming so a lot of this is gibberish

    I can sort of understand what you mean. I have read a lot of FAQ's about generating random numbers and thought of a lot of ways to integrate it into code. A question i have that dosnt seem to be answered anywere is can i just put in the function rand() to generate a random number or is there more to it? for instance is all the seeding and returns and stuff necessary to produce a single random number?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seed rand with something so that you don't get the same random chain of numbers every time you run it. Then call rand when you want it to give you another number. The FAQ should cover all of that.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Ok well ive tried to get random numbers in there, but now the grid isnt printing at all.....

    Code:
    # include <stdio.h>
    # include <math.h>
    # include <stdlib.h>
    # include <time.h>
    
    int lx;
    int ly;
    char grid [20] [20];
    int z;
    int j;
    int n;
    int i;
    
    int main()
    
    {
    
    printf ("Insert the width (Lx) and height (Ly) of the grid \n");
    scanf ("%d %d", &lx, &ly);
    printf ("insert the number of randomly generated points \n");
    scanf ("%d", &n);
    
    for (i = 0; i < n; i++)
      {
          srand (time (NULL));
           i = rand();
           
        return(0);
      }
    
    for (z = 0; z < lx; z++)
    
      { 
       for (j=0; j < ly; j++)
    
        {
          grid [rand()] [rand()] = '.';
    
        }
       }
    
    for (z = 0; z < lx; z++)
    
      {
       for (j=0; j < ly; j++)
      
        {
         
         printf ("%c", grid [z] [j],);
         
        }
        printf ("\n");
       }
    
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should fill your grid with spaces before adding dots to it. You also don't need that first loop. Just call srand one time. Then, when you need a random number, call rand once per number you need, at the time you need it to give you a new number.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    At the risk of repeating my question from the last thread about this code...
    What happens if the operator enters a 50 x 50 grid?
    You could have some real array bounds problems.

    Always count on an operator to do stupid things... like entering 40584 x 395334 and figure out a way to either A) trap the answer within your array bounds or b) expand the array to suit.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    @ commontater: yea sorry for not replying last time, the users that will be testing my program most likely wont go higher than 10 by 10. Maybe once i have completed the important parts i will come back to this. Thank you for pointing it out though. I think i might just change the print to say "2 numbers less than 10" for example.

    Also i deleted my last post because i have redone the program in a way i think is better. The grid now prints again if i take away the "[rand() % z] [rand % j]" part. With this in though the output says there is a floating point exeption. any idea what this is?

    Code:
    # include <stdio.h>
    # include <math.h>
    # include <stdlib.h>
    # include <time.h>
    
    int lx;
    int ly;
    char grid [20] [20];
    int z;
    int j;
    int n;
    int i;
    
    int main()
    
    {
    
    printf ("Insert the width (Lx) and height (Ly) of the grid \n");
    scanf ("%d %d", &lx, &ly);
    printf ("insert the number of randomly generated points \n");
    scanf ("%d", &n);
    
    srand (time (NULL));
    i = rand(); 
    
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
    
         { 
          grid [z] [j] = '.';
          grid [rand() % z] [rand() % j] = 'x';
         }
        }
    
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
      
        {
         
         printf ("%c", grid [z] [j]);
         
        }
        printf ("\n");
       }
    
    }
    I feel so close to the finish line really hoping somebody can nudge me to the end.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
    
         { 
          grid [z] [j] = '.';
          grid [rand() % z] [rand() % j] = 'x';
         }
        }
    You are trying to do 2 things at the same time that you shouldn't be. You are filling X*Y random squares with 'x', at the same time you are filling the current square with a '.'.

    Loop through and set everything to '.' first. Loop again (however many times you want) and set that many random spots to 'x'.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Ok its generating a grid and randomizing the positions of the x's so thats great
    Now i have to make it generate as many x's as the scanned n value. For instance, say i insert 5 for n, there should be 5 randomly distributed x's in the grid. for some reason the loop ive produced to do this isnt working correctly. i cant find a way to limit the number of x's.

    Also youll notice ive used [rand() % lx] [rand() % ly] instead of z and j because when i use z and j (instead of lx and ly) it says there is a floating point exeption. still dont know why it does that?

    Code:
    # include <stdio.h>
    # include <math.h>
    # include <stdlib.h>
    # include <time.h>
    
    int lx;
    int ly;
    char grid [20] [20];
    int z;
    int j;
    int n;
    int i;
    
    int main()
    
    {
    
    printf ("Insert the width (Lx) and height (Ly) of the grid \n");
    
    scanf ("%d %d", &lx, &ly);
    
    printf ("insert the number of randomly generated points \n");
    
    scanf ("%d", &n);
    
    srand (time (NULL));
    
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
    
         { 
          grid [z] [j] = '.';
    
         }
        }
    
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
    
         { 
          for (i = 0; i < n; i++)
    
           {
           grid [rand() % lx] [rand() % ly] = 'x';
    
           }
         }
        }
    
    for (z = 0; z < lx; z++)
    
      {
       for (j = 0; j < ly; j++)
      
        {
         printf ("%c", grid [z] [j]);
         
        }
        printf ("\n");
       }
    
    return (0);
    
    }

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    No. You only want to generate n random coordinates. Your current use of a triple nested loop would generate lx * ly * n coordinates that are 'x'. You want just the inner part of that loop:
    Code:
    for (i = 0; i < n; i++) {
        grid[rand() % lx][rand() % ly] = 'x';
    }
    But that has a problem that you may (not likely, but you may) end up with putting an 'x' in a spot that already has an 'x', thus your grid will have fewer actual x's than expected. You need to only put an 'x' somewhere there isn't already one:
    Code:
    for (i = 0; i < n; i++) {
        do {
            x = rand() % lx;
            y = rand() % ly;
        } while (grid[x][y] == 'x');  // keep looping until we find an "empty (.)" grid coordinate to put our 'x'
    }

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Wow thank you all so much. ive finally managed to do it after 8 hours of solid work

    All this help is much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a random grid from file into an array
    By seandil666 in forum C Programming
    Replies: 9
    Last Post: 12-08-2009, 08:27 AM
  2. Generating Random Numbers
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2009, 08:01 AM
  3. Grid generation from scattered points?
    By canada-paul in forum C Programming
    Replies: 6
    Last Post: 12-12-2003, 05:46 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. Generating Random Numbers
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2002, 06:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21