ajsmith5150
ajsmith5150

Reputation: 29

creating function, outside main() function, for random Number in specific range

I am trying to write a function for a random number in a range decided by the user. I call srand() in int main() with the function RandomNumber defined above.

I know if do the randomnumber equation in main(), while also calling srand() in main(), it works how I want.

This is the program that does not do what I want. The return value is a combination of numbers and letters. I need return of a number in a range determined by the user input. Essentially, I need to create a random number generating function outside the int main() function, then be able to call the RandomNumber inside int main(). I run into trouble with calling srand(time(NULL));. I have to call srand() inside main() or the random number is not generated correctly.

#include <iostream>
#include <ctime>
using namespace std;

int RandomNumber(int userNum1, int userNum2) {

return (rand() % (userNum2 - userNum1) + userNum1);
}

int main()
{   
    srand(time(NULL));
int userNum1;
int userNum2;
cin >> userNum1;
cin >> userNum2;



cout << RandomNumber << endl;

return 0;
}

Even if I call srand() inside the RandomNumber function, it does not work as I want. /////////////////////////////////////////

This is a program that does what I want, but I am trying to define the function RandomNumber above int main().

#include <iostream>
#include <ctime>
using namespace std;




int main()
{   
    srand(time(NULL));
    int RandomNumber;
    int userNum1;
    int userNum2;
    cin >> userNum1;
    cin >> userNum2;


    RandomNumber = rand() % (userNum2 - userNum1) + userNum1;
    cout << RandomNumber << endl;

    return 0;
}

///////////////////////////////////////////// How can I create my RandomNumber function outside main() and call it insdide main()?

Upvotes: 1

Views: 225

Answers (3)

ajsmith5150
ajsmith5150

Reputation: 29

I was able to figure out the most streamlined version, in my opinion.

#include <iostream>
#include <ctime>
using namespace std;

int generateRandomNumber(int lowNumber, int highNumber) {
    int randomNumber = rand() % (highNumber - lowNumber) + lowNumber;
    return randomNumber;
}

int main() {
    srand(time(NULL));
    int userNum1;
    int userNum2;

    cin >> userNum1;
    cin >> userNum2;

    cout << generateRandomNumber(userNum1, userNum2) << '\n';

    return 0;
}

Upvotes: 1

anatolyg
anatolyg

Reputation: 28290

The problem is: if RandomNumber is a function, cout << RandomNumber tries to print the function's address in memory (it's actually unspecified; on some systems, it just prints 1 to acknowledge the fact that the function exists).

To call a function, provide a list of arguments:

cout << RandomNumber(userNum1, userNum2)

Even if a function doesn't get any arguments, you have to provide parentheses to call it. For example:

void do_useful_stuff() // no arguments
{
    puts("Useful Info");
}

int main()
{
    cout << do_useful_stuff; // doesn't print anything useful
    cout << do_useful_stuff(); // prints "Useful Info"
}

In the code which works well

RandomNumber = rand() % (userNum2 - userNum1) + userNum1;
cout << RandomNumber << endl;

RandomNumber is not a function but a number. Printing it will work without any surprises.

Upvotes: 2

Mshnwq
Mshnwq

Reputation: 1

#include <iostream>
#include <ctime>
using namespace std;

// Declare the function to generate a random number
int generateRandomNumber(int lowerBound, int upperBound);

int main()
{
    srand(time(NULL));
    int userNum1;
    int userNum2;

    cin >> userNum1;
    cin >> userNum2;

    int RandomNumber = generateRandomNumber(userNum1, userNum2);
    cout << RandomNumber << endl;

    return 0;
}

// Define the function to generate a random number
int generateRandomNumber(int lowerBound, int upperBound)
{
    int randomNumber = rand() % (upperBound - lowerBound) + lowerBound;
    return randomNumber;
}

Upvotes: 0

Related Questions