# Thread: random functions, is it possible?

1. ## random functions, is it possible?

I was going to start text based game for practice, and while i was just thinking about how i could make it all work, i was wondering is there anyway to call a random funciton? like if i make a random number 1-5 if its 1 rat() is called ect?... i seems every time i poste a question i come up with a good answer half way through, but any help is apreciated

2. Does this not work?:
Code:
```int num = 1 + (5.0 * rand()) / RAND_MAX;
if (num == 1) rat();
else if (num == 2) fat();
else if (num == 3) cat();
else if (num == 4) tru();
else dat();```
Or, alternately, use a table of function pointers and lookup the function. Or use a switch statement.

3. thats the code i was talking about being a possible answer, i haven't started on the program yet, i kinda just work up and decided to make a game... ill try and i gotta get my gf right now so ill be back at the comp in a while

4. what headers do i include to do random numbers? (i've never used random numbers before, and the tutorial on this site isn't very helpful)

5. rand() and srand() are declared in <cstdlib>. A lot of people like to seed the RNG with the system time, and you can find time() in <ctime>. Here's the most common way to make random numbers:
Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
srand((unsigned)time(0));

for (int i = 0; i < 20; i++)
cout << rand() % 10 << ' ';
cout << '\n';

return 0;
}```
You should know that this way sucks ass. Using time() as the argument to srand() has subtle issues, rand() isn't a good RNG to begin with, and using % to force the numbers into a smaller range makes it even worse. That's your fair warning, but for simple stuff it really doesn't matter because it'll look random enough.

6. i compiled your source, and it worked fine, but where do you declare rand()? and where do you declare int i ?(im not real good with technical terms, but i think declare is the word im looking for)

edit: duh (i should read more lol rand() is declared in cstdlib, but what about int i?

7. >>rand() is declared in cstdlib, but what about int i?

the int data type is "built in", so to speak, along with double, char, float, etc. You might be able to find details about the type in one of the major header files like iostream.

The variable i is declared in the following line:

for (int i = 0; i < 20; i++)

If you have an up to date compiler this should restrict the scope of the variable i which is of type int to the for loop within which it is declared.

8. so would it work just the same declaring the variable int i somewhere else?

9. My personal preference for such a task would be to use a std::map from the key type (integers) to a "task" object pointer, where the task object is an abstract base for your functionality. This allows for a great deal of flexibility, without too much work.

Task could define an appropriate interface, and then subclasses would implement specific functionality. A good hierarhcy would be something like: