Random Integer

Hi.
I'm new to C++ and new to these forums.
I have coded in Warcraft 3's language JASS which, if I understand correctly, is a form of C. (Or based of it, or something.)

I'm just messing around right now, and I tried to make a function that returns a random integer.
I'm planning to call this function from another function later on, providing two integers (low and high) and then have the function return a random integer between the two values.

In JASS this "function" looks like this:
native GetRandomInt takes integer lowBound, integer highBound returns integer


I googled around and find a few threads and tutorials on how to get a random integer.
I finally made it work, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstdlib>
using namespace std;

int stay()
{
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
}

int main()
{
    int i, r;
    srand (time(NULL));
    r = rand();

    for(i = 0; r >= 50; i++)
    r = rand();

    cout << "Random number: " << r;

    stay();
    return 0;
}


(stay is simply to prevent the output window from automatically closing)


TL;DR:
Finally to my question:
How do I make a function take values and return another?
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int i, r, low, high;   //Create some sort of variables that will later be used in the function
    srand (time(NULL));
    r = rand();

    for(i = low; r >= high; i++)
    r = rand();

    return r;   //Return the random integer
}


int whatever()
{
    //Random code..
    int random_number = main(1,10);    //I want this to define low and high in main(), and then give me a random number between 1 and 10.
}


Seeing as this question was unanswered...

Line 17 is a line that will have your compiler bashing you over the head several times with a giant hammer. However, I see what you want to do.

Firstly, I'd suggest you look over functions more carefully, and recognize that a standards-compliant C++ program starts with main() without any exceptions, so you'll want something along the lines of what's in whatever() in main and vice-versa.

Tutorial:
http://cplusplus.com/doc/tutorial/functions/

Good luck!

-Albatross

P.S: Oh, and check your for loop. Are you sure it does what you intended?
Topic archived. No new replies allowed.