Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random number generation using C code

    Dear all, currently i'm doing a snake game, but i don't know how i can generate the food randomly when the snake eat it. Can someone help me?

    I know i need to generate a random number to light up the led/food with x and y co-ordinate, but how should i do it?

    I'm using ledrow[0] and ledcol[0] for the food co-ordinate.

    Pls help
    Thanks in advance
    west_dweller

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    You can use the rand() function from the standard libraries. You will most likely need to scale it within your bounds of 0,MAX_ROW and 0,MAX_COL so here is a good page explaining how to use the rand() function.

    http://members.cox.net/srice1/random/crandom.html
    Omnis mico antequam dominus Spookster!

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Make sure you don't forget to seed the random number with srand(), or else the "random" numbers will always come out the same! Use the following:

    Code:
    #include <stdlib.h>
    
    void main()
    {
    srand( (UNSIGNED)time(NULL) ); // <- Borrowed from MSDN
    }
    Put that line in your main (or WinMain) function and link in the stdlib.h and you'll be good to go!

  • #4
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, i still don't know who to generate the number randomly. This is the code that i copy from the web and i did some modification.

    void setfood(void)
    {
    int seed;
    double r, x;
    int y, z;

    seed = 600;
    srand(seed);

    r = ( (double)rand() / (double)(RAND_MAX+1) );
    x = (r * maxcol-1);
    y = -1*((int) x);
    z = (y + 1);

    printf("random number %5d %5d\n",y,z);

    row[0] = y;
    col[0] = z;
    }

    Also what is the different between srand and rand ???

    Thanks in advance
    west_dweller

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    srand() seeds the number generator.
    rand() generates a random number.

    Given a seed, and some fixed constants a set of mathematical operations are recursively performed. I believe they use modulus and some other operations in these sequence of operations.

    Code:
    void setfood(void)
    {
    int x, y;
    x = y = 0;
    
    x = rand()%MAX_COL+1;
    y = rand()%MAX_ROW+1;
    
    col[0] = x;
    row[0] = y;
    }
    
    int main() {
    srand(time(NULL));
    return 0;
    }
    rand() will return a *random* number between 0 and RAND_MAX.
    rand()%MAX_COL+1 will return a number between 0 and MAX_COL.
    rand()%MAX_ROW+1 will return a number between 0 and MAX_ROW.

    Continually seeding the generator with a changing seed, in this case it's time() (which is the number of seconds since Jan 1, 1970), changes the outcome of the mathematical operations and hence a new number.

    [edit:] Realised a mistake or two, I only just woke up.
    Last edited by Mhtml; 02-15-2005 at 04:20 AM.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •