Thread: Random number generation problem to generate co-ordinates in a range

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    21

    Random number generation problem to generate co-ordinates in a range

    Hello,

    I am woking on a program which requires the generation of two random numbers to be used as X and Y co-ordinates within a certain range. I have found a random number generator for this:

    Code:
    int randLim (int min, int max){
        
        int randNo;
        srand(time(NULL));
    
        randNo = (rand() % (max - min + 1) + min);
        
        return randNo;
    }
    And I use it here:

    Code:
            int randomX = randLim(1,mapX);
            //Sometimes put a Sleep(); command in here
            int randomY = randLim(1,mapY);
    The problem I am having is that most of the time the two numbers are the same, and even if I put in a slight delay to make them different they end up looping through the same numbers over and over (usually going up in 3's or 4's)

    I was wondering if anyone had any better ideas to use for a random number generator or if I am going about this wrong.

    Many thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Move this line from randLim:
    Code:
    srand(time(NULL));
    to near the start of your main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    Wow, not only does it work it is also a lot faster!

    How exactly does it work better this way?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, you've started actually using rand() to get the next number in the pseudorandom sequence, instead of continually re-seeding and getting only the first number in each pseudorandom sequence
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    Although this is working better, I'm not sure it has fixed the skipping numbers problem.

    I.e certain co-ordinate combinations may never be generated

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sephmk
    certain co-ordinate combinations may never be generated
    That is true, much like how it is possible to toss a fair coin a hundred times and never get tails. Also, if mapX and mapY are larger than RAND_MAX, then the range of random numbers generated could be smaller than the desired range, so it would be like rolling a six sided die to choose between seven options.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    I'm running it at the moment and its 20x20 and sometimes it does not complete, however this may be an error in other parts of the code. I am looking through as I type this and shall update this thread after I have had a good scour.

    Thanks for all your help so far though =)

  8. #8
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    In which purpose u r making this bro!.. Is this a graphic based programme!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 05:49 PM
  4. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  5. Replies: 10
    Last Post: 11-23-2001, 10:01 AM