Thread: Random Array map

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    85

    Random Array map

    hi everybody

    i have a map here 32 X 32, 1 = food , 0 = no food
    I have postioned the food on the map like follow:

    Code:
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE] =
      { {0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0},
        {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0},
        {0,0,0,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
        {0,0,0,1,1,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
      };
    
    char trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
    
    
    void output_program(struct prog *progp)
    {
      char *codep = progp->code;
      char token;
    
      while (token = *codep++)
         fputs(token_table[token].name, stdout);
      putchar('\n');
    }
    
    
    void print_matrix(void)
    {  int i, j; char symbs[] = " OX";
    
       for (i=0; i < MATRIX_SIZE; i++)
       {  for (j=0; j < MATRIX_SIZE; j++)
              putchar(symbs[trace_matrix[i][j]]);
          putchar('\n');
       }
    }
    Now i am looking to create a random array, so everytime it runs food place into different place.


    100 food i am needed and 32 X 32 grid map.


    Thanks for evey1's help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think every post from the last thread is still applicable: why should trace_matrix and matrix_data be related; and to generate random food, generate a row and a column using rand until you hit a square with no food. (To get a number between 0 and 31, you can use rand()%32.)

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Becasue this program is going to show by XML trace_matrix will be use later on the XML Tree

    Really thaks for you quick response. I am really not trying to ask some1 to code for me.

    But any1 know any similar example i can learn ??
    thanks

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Hi I think I need to change my mind now, coz if i randomly add food
    than the XML structre will be completely difference??

    original working code attached

    cherrys every1

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by peacealida View Post
    Hi I think I need to change my mind now, coz if i randomly add food
    than the XML structre will be completely difference??
    I'm pretty sure nothing could be further from the truth. All the XML structure is, is formalizing your data. The data hasn't changed -- it's still 1 for food and 0 for not, in cells, in a grid.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Thanks god.
    I will play around it tonight. see how far it goes.
    I will follow up in the morninging
    thanks tabstop

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    tabstop how much time you have spent to be a programmer???

    I think you must working hard hard

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by peacealida View Post
    tabstop how much time you have spent to be a programmer???

    I think you must working hard hard
    What makes you think I'm a programmer?

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    If you are not programmer or some kind of computer related job, how can you reply the anser so quick??

    Don't tell me you are 10 years old boy just doing it for fun please
    I will kill myself

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    About the rand()&#37;30 function, Is there an option to random between specific numbers that I select? like for example: between 100 to 300 only. meaning sparing 0-99.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sy84n View Post
    About the rand()&#37;30 function, Is there an option to random between specific numbers that I select? like for example: between 100 to 300 only. meaning sparing 0-99.
    You're supposed to start your own thread to ask for help. But I can see you're new here so I'll cut you some slack this first time. Make sure you read the FAQ etc before next post anyway.
    The answer is simple, adding 100 to your random number will make sure it is greater than 99.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    OK. first - thank you
    I wrote it this way: number = 100 + rand()&#37;300 + 1;
    It did not work well, the random stays the same every time I run it.

    also tried: number = rand()%300 + 100;
    didn't work also. ( I use Dev-C++ Compiler, maybe it is related?)

    I'll read the FAQ.
    Last edited by sy84n; 03-29-2008 at 06:25 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It did not work well, the random stays the same every time I run it.
    I'm sure there's something in the FAQ about this, but here it is anyway: you have to call srand() to initialize the random number generator. Usually one initializes it with the current time. Not only that, but you can only call this function once.

    If you don't seed the random number generator, you will indeed get the same numbers every time.

    Think for a moment about what this does.
    Code:
    number = 100 + rand() &#37; 300;
    It calls rand(). It uses modulus to restrict this number to the range [0,300). Then it adds 100 to the number. In effect, you have a number in the range [100,400). (From 100 to 400, including 100 but not 400. Or from 100 to 399, inclusive.)

    Perhaps you don't know what modulus does. It returns the remainder of a division. So n % 3 means "divide n by three and return what was left over" -- using integer division, of course. In effect, this means that by modulus-ing by 3, you'll get either 0, 1, or 2. Think about it.

    All of this means that the formula you want is probably
    Code:
    n = 100 + rand() % 200;
    for 100 to 199 (inclusive), or
    Code:
    n = 100 + rand() % 201;
    for 100 to 200, or
    Code:
    n = 101 + rand() % 200;
    for 101 to 200 (again, inclusive).

    Food for thought.
    Code:
    #include <stdio.h>
    #include <stdlib.h>  /* for srand() and rand() */
    #include <time.h>  /* for time() */
    
    int main() {
        int x;
    
        /* Seed the random number generator. */
        srand((unsigned)time(0));
    
        /* Generate and print ten random numbers. */
        for(x = 0; x < 100; x ++) {
            int n = 100 + rand() % 200;
            printf("%d\n", n);
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    So I have to creata 300 random numbers bewteen 1-1024?
    But how do I assigned value "1" into the array field??

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    85
    Is this make any sense?
    what i want is to fill 300 1s into the array field
    thanks for the help XX


    Code:
    
    char matrix[MATRIX_SIZE][MATRIX_SIZE];
    char matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}};
    int main()  {
        int x;
        srand(time(0));
    
        for(x=0; x<[MATRIX_SIZE]; x++) {
            int n = 100 + rand() &#37; 300;
            printf("%d\n", 1);
        
        for (x = 0; x < 300; x++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1;
            
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 06:53 PM
  2. random fill array
    By zeromx in forum C Programming
    Replies: 7
    Last Post: 11-07-2006, 06:18 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM