# Code Explanation

• 09-24-2009
Aliaks
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; };```
• 09-24-2009
Dae
This is probably what you're going to get as the first reply:

What do you think it does?
• 09-24-2009
rogster001
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
• 09-24-2009
Dae
Quote:

Originally Posted by rogster001
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.
• 09-24-2009
rogster001
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
• 09-24-2009
Dae
Quote:

Originally Posted by rogster001
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?
• 09-24-2009
laserlight
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.
• 09-26-2009
Aliaks
I did not write this, I have come across it while brousing and don't understand it proparly
• 09-26-2009
EVOEx
Quote:

Originally Posted by Dae
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
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...
• 09-26-2009
Dae
Quote:

Originally Posted by EVOEx
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. :D
• 09-26-2009
Baje
hello
• 09-26-2009
Aliaks
Ok well I am none the wiser of what this does :(
• 09-26-2009
tabstop
Quote:

Originally Posted by Aliaks
Ok well I am none the wiser of what this does :(

randBetween = rand + Between (low and high).
• 09-26-2009
Noise
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.
• 09-26-2009
abachler
Quote:

Originally Posted by Dae
What do you think it does?

I think it makes hot steaming bacon :)