Code Explanation

This is a discussion on Code Explanation within the C++ Programming forums, part of the General Programming Boards category; Hey guys, just wondering if anyone could give me an idea what this code does? Code: static int randBetween(int low, ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Code Explanation

    Hey guys, just wondering if anyone could give me an idea what this code does?

    Code:
    static int randBetween(int low, int high)
    {
        if (low == high) return low;
        if (low > high)
        {
            int tmp = low;
            low = high;
            high = tmp;
        }
    
        static bool firstTime = true;
        if (firstTime)
        {
            srand((unsigned int)time(NULL));
            firstTime = false;
        }
        int num = rand();
        int result = num % (high - low + 1) + low;
        return result;
    }
    
    class QuestionGenerator
    {
        
    public:
        virtual const string& poseQuestion(void) = 0;
        virtual bool isCorrectAnswer(const std::string& answer) const = 0;
    
    };

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    This is probably what you're going to get as the first reply:

    What do you think it does?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    try compiling it and see what the output is, i suspect you will be disappointed with the results unless you move this >
    Code:
    srand((unsigned int)time(NULL));
    because it should only be called once and it looks as though that function randbetween() would be used more than once

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by rogster001 View Post
    i suspect you will be disappointed with the results unless you move this >
    Code:
    srand((unsigned int)time(NULL));
    because it should only be called once and it looks as though that function randbetween() would be used more than once
    Which is why it's only being called when firstTime is true, and it's only true the first time the function is called, because it's set to false afterwards (and it's static so it isn't reset).

    Code:
         static bool firstTime = true;
        if (firstTime)
        {
            srand((unsigned int)time(NULL));
            firstTime = false;
        }
    P.S. Doesn't this topic, or one very similar, already exist? I remember that interface.
    Last edited by Dae; 09-24-2009 at 04:18 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    this is true, true, but why wouldnt it just be called from main and be done with it? if seeding random number is only ever done once i mean
    Last edited by rogster001; 09-24-2009 at 04:21 AM.

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by rogster001 View Post
    this is true, true, but why wouldnt it just be called from main and be done with it?
    I don't know, but that has nothing to do with what you said. What does the OP have to say?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by rogster001
    why wouldnt it just be called from main and be done with it?
    Because the function author has the misguided idea of making sure that the PRNG is always seeded with srand(). I call it misguided because it means that one cannot (without modifying the function) set the seed to be something fixed so as to say, repeat an experiment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I did not write this, I have come across it while brousing and don't understand it proparly

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Dae View Post
    This is probably what you're going to get as the first reply:

    What do you think it does?
    Close. That was PART of the first reply. The actual first reply was:
    Quote Originally Posted by Dae View Post
    This is probably what you're going to get as the first reply:

    What do you think it does?
    But then again, if you would've guessed it properly you would've been stuck in an infinite recursive loop. And if your stack would've been big enough, you'd still be writing your post now...

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by EVOEx View Post
    Close. That was PART of the first reply. The actual first reply was:


    But then again, if you would've guessed it properly you would've been stuck in an infinite recursive loop. And if your stack would've been big enough, you'd still be writing your post now...
    EVOEx, you just blew my mind.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    1
    hello

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ok well I am none the wiser of what this does

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Aliaks View Post
    Ok well I am none the wiser of what this does
    randBetween = rand + Between (low and high).

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    Well the function randBetween will return a random integer between 'low' and 'high'. For example if you called it like this:
    Code:
    int x = randBetween(1, 6);
    x would equal a random number between 1 and 6.



    So.
    Code:
    if (low == high) return low;
    This is just saying, if you input the same number twice just return that number.

    Code:
    if (low > high)
        {
            int tmp = low;
            low = high;
            high = tmp;
        }
    This code simply swaps around the low and the high values if they were in the wrong order, eg, if low was 6 and high was 1, it would swap them.

    Code:
    static bool firstTime = true;
        if (firstTime)
        {
            srand((unsigned int)time(NULL));
            firstTime = false;
        }
    This generates the 'seed' which the function rand() requires. It uses the current time so set the seed. Each time rand() is called subsequently, the seed is changed to the next one in the sequence. Look up random number generators if this doesn't make sense.

    Code:
    int num = rand();
        int result = num % (high - low + 1) + low;
        return result;
    This generates the random number and returns it. 'num' gets set to a completely random integer. Then you take the modulus of 'num' and (high - low + 1) to get a number between zero and the difference high - low. Then add 'low' and you have a number between low and high.
    eg
    Code:
    randBetween(1, 6);
    and num gets set to 12753 by rand().
    (high - low + 1) = 6.
    num % 6 = 3 (check with a calculator)
    3 + low = 4 and that is the number that gets returned.
    Last edited by Noise; 09-26-2009 at 11:05 PM.

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Dae View Post
    What do you think it does?
    I think it makes hot steaming bacon
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 09:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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