# Thread: Random Phrase

1. ## Random Phrase

I have been looking at teh tutorials for random numbers and was wondering if it was possible to get random phrases or would I just get a random number and if it =s something then post a certain phrase?

And can I use a for loop witht this code from the FAQ?
Code:
```#include <iostream>
#include <ctime>

int main(void)
{
int i;

srand(time(NULL));
i = rand();
std::cout <<"Your random number is " <<i <<std::endl;
std::cout <<"This compiler can generate random numbers from 0 to "
<<RAND_MAX <<std::endl;

return(0);
}```

2. >> would I just get a random number and if it =s something then post a certain phrase?

Yes, that's how you would do it. To do so you need to get a number within the range of how many phrases you've created. There should be info on how to generate a number within a specific range in the FAQ. Then, use a switch or a map or an array/vector to determine which phrase is displayed based on the random number generated.

3. Yes, I think the one in the FAQ over complicates things by increasing the range....so I'm not sure.

4. The formula is the important part. You can just use that to get a number in a range.

5. Code:
```#include <iostream>
#include <ctime>

int main(void)
{
int i;

for (i = 0; i < 20)
{
srand(time(NULL));
i = rand();
std::cout <<"Your random number is " <<i <<std::endl;
}

return(0);
}```
Would that work? I have tried to adapt the code in teh FAQ

6. Work for what? Getting a number in a range? No. First, srand() should be called once for the whole program, like you did it in your first piece of code from the FAQ. It is inside the for loop in your second piece of code, so you should put it back. Second, you don't need a loop to get a single random number within a range. That loop code is just an example to show how you can get a bunch of random numbers. Look for the Formula (marked as Formula in the code comments). Just replace the i = rand() in your first piece of code with the new way of calculating i. Don't forget that you have to change min and max in the formula to whatever values you want to be the min and max of your range.

7. So I can set a RAND_MAX and RAND_MIN? Or how do I set a range?

8. Do you know the Formula I am talking about in the FAQ? Find that and read the comments and see if you can understand what I'm talking about. BTW, this is the FAQ I'm referring to:

http://faq.cprogramming.com/cgi-bin/...&id=1043284385

9. Is that the one where you set the minimum and maximum in GetRand?

10. Originally Posted by FAQ
/*
* Formula:
* rand() % N <- To get a number between 0 - N-1
* Then add the result to min, giving you
* a random number between min - max.
*/
rc = (rand() % (max - min + 1) + min);
Isn't that exactly what you are looking for?

11. It's the code underneath the comment that says, "* Formula:".

12. rc = (rand() % (max - min + 1) + min);

THat?

13. That is a formula for finding a random number within a range of values. I hope that's what you are looking for. That is the first step in outputting a random phrase.

If you know what % does then you can figure out how it works.

14. I'm afraid I am too nooby to know what percent does...unless I have overlooked it. Can I just use int min and int max to go in that function thing?

15. Originally Posted by bumfluff
I'm afraid I am too nooby to know what percent does...unless I have overlooked it. Can I just use int min and int max to go in that function thing?
What do you think that function is? Can't you see it's user-defined at the bottom of the program with that formula?

% is an integer operator that returns the remainder of division

4 % 2 = 0
5 % 2 = 1
11 % 3 = 2

So you call rand, you mod it by the max number and get the remainder (which is always smaller than your divisor) and subtract your mininum number + 1. Then you add to all that your minimum.

rand() % (max - min + 1) + min

Popular pages Recent additions