Thread: I need help using the rand() function

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    I need help using the rand() function

    First of all yes I read the function on this site. That was console app, this is win32/directx. Ok what I want to do is generate a random number and assign it to the X1 coordinate of a rectangle, you know to blits and image.

    I have tried several things and none seem to work.

    First off I have a Meteor Class.

    and I am assinging the X1 values to the rectangle via the member function

    EnemyMeteor.SetX1Coordinate(); where EnemyMeteor is a meteor object.

    I have trie doing this

    int RandomX1=rand()%745;

    EnemyMeteor.SetX1Coordinate(RandomX1);

    or


    EnemyMeteor.SetX1Coordinate(rand()%745);

    yes and I am included #include <stdlib.h> This is my first time really using this function......please help.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    30

    What's the problem?

    Do you get a compiler error, does it not function properly or what? If the problem is that rand always gets the same numbers, then you need to seed it. If the problem is something else, you'll have to give more info abput it.

    To learn more about rand() and seeding it etc. go here
    "You... Remarkably made it rain. Rain of blood." - Tomoe

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >That was console app, this is win32/directx

    Therefore there is a difference in how information is displayed which has absolutely nothing to do with the rand() function.

    What's the problem? Is your code producing errors? If so, what errors? Is your code behaving in an unexpected manner? If so, why isn't it behaving as you expect?

    In the absense of this info I'm going to take a wild stab in the dark. Have you seeded the random number generator with srand()?

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Re: What's the problem?

    Originally posted by ninja
    Do you get a compiler error, does it not function properly or what? If the problem is that rand always gets the same numbers, then you need to seed it. If the problem is something else, you'll have to give more info abput it.

    To learn more about rand() and seeding it etc. go here
    yeah seems to always have the same number.......
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Don't really know what this "seeding" thing is all about......

    it always get the same number about 50 or so.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok I am seeding with Time


    srand(time(NULL));
    int RandomX1;
    RandomX1=rand()&500+100;

    EnemyMeteor.SetX1Coordinate(RandomX1);


    But maybe because this is using the time when randomization are not, well the numbers don't seem to be apart enough. I need good randomization for pixels between 0 and 800 pixels or so. What could I do? I am using MSVC++
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    or you could just use randomize().

    eg.

    type function()
    {
    int x1;
    randomize();
    x1 = (rand()%750)+1;

    return type;
    }

  8. #8
    I too need something to give more random of numbers. I seed my rand() with srand(unsigned(time(NULL))), and I use my own function I made to work with rand

    Code:
    int random(int lowNumber, int HiNumber)
    {
      return lowNumber + (rand() % HiNumber);
    }
    it works fine, it comes up with random numbers and all. The only problem I got with it, is the numbers aren't random enough. I am making a Vertical shooter with Allegro 4.01, and I just started the AI. I am using a Finite State Machine based on a switch statement. It works and compiles right, but all the enemies do just about the same thing all the time, because the numbers aren't coming up random enough.

  9. #9
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by frenchfry164
    I too need something to give more random of numbers. I seed my rand() with srand(unsigned(time(NULL))), and I use my own function I made to work with rand

    Code:
    int random(int lowNumber, int HiNumber)
    {
      return lowNumber + (rand() % HiNumber);
    }
    it works fine, it comes up with random numbers and all. The only problem I got with it, is the numbers aren't random enough. I am making a Vertical shooter with Allegro 4.01, and I just started the AI. I am using a Finite State Machine based on a switch statement. It works and compiles right, but all the enemies do just about the same thing all the time, because the numbers aren't coming up random enough.

    hehe yup sounds like the problem I am having.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  10. #10
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by The Dog
    or you could just use randomize().

    eg.

    type function()
    {
    int x1;
    randomize();
    x1 = (rand()%750)+1;

    return type;
    }

    I am not sure if MSVC++ allows the use of randomize. I doesn't seem to work for me.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    30

    I don't know,

    but try to place the seed-code elsewhere. I've had the problems you're having and sometimes solved it by placing the seed somewhere else. Hope that helps...
    Last edited by ninja; 06-01-2002 at 06:24 PM.
    "You... Remarkably made it rain. Rain of blood." - Tomoe

  12. #12
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    1.) http://www.cprogramming.com/cboard/s...threadid=18354

    2.) Use the <cstdlib> library instead of <stdlib.h>

  13. #13
    that link doesn't help at all.

    I just need a way to make the numbers more random. It would work for something like ErionD's fighting game, where AI is only applied to one thing at a time, but in mine, over 20 things are applied, and they all do the same thing at the same time almost!

  14. #14
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    If you repeatedly seed the number generator with the time just before you generate a random.. it's going to generate the same random number of that short period of time (until the time changes). Seed once at the start of your program, and that's it.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  15. #15
    I already figured that out, but it still doesn't give me random enough of numbers. If you looked at my function, it doesn't seed in the function. I have the seed in main.

Popular pages Recent additions subscribe to a feed