Functions...Again

How do I turn this into a function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ctime>
#include <iostream> 
 
using namespace std; 
 
int random (int min, int max);

int main()
{ 
     srand(time(NULL)); 
     cout <<  rand() % 10 + 1 << endl; 
 
     return 0; 
}
Last edited on
http://cplusplus.com/reference/clibrary/cstdlib/rand/
Read the above it is important.
you only need to think about what you need a function to do before you figure out how to write it, you asked originally before you edited your post, that the function needed to be in a certain range, and that it gave you a number.

This is your standard pseudocode. you wanted a number, so the funciton should probably return a number: so it's return type will be int. You also wanted it to be in a certain range, so it should probably take a parameter of what the range you want.

return type, name, ( parameters ) { /* stuff */ }
int aFunction ( int range )

This just requires a little math from a standard rand function. The standard function will still give you a number between 1 and n, which is what your asking for, but what if you need a number between 43 and 78...?
1
2
3
4
5
6
7
8
9
10
11
//standard function
int std_Random( int range )
{
    return (1 + rand() % range);
}

// ranged n-n
int rangd_Random ( int start, int end )
{
   return  ...
}
Last edited on
I think i get it now. thanks alot :)
Topic archived. No new replies allowed.