Need help with a random number generator function.

I need to come up with a program that will randomly generate a number within the range of a user-inputted min and max. I have this so far but I can't figure out what I've done wrong.

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
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
using namespace std;

int getRandomNumber (int min, int max);

int main()
{
	int seedNumber, z;

	cout << "enter the seed for the random number generator: ";
	cin >> seedNumber;
	cout << endl;
	srand(seedNumber);
	
	cout << "enter the minimum and maximum size of the random numbers: ";
	cin >> min, max;
	cout << endl;
	cout << endl;
	z = getRandomNumber(min, max);
	
	cout << "random number = " << z;
	


}

int getRandomNumber (int min, int max)
{
	int guess;
	guess = (rand() % (max - min + 1)) + min;
	return guess;
}
I have this so far but I can't figure out what I've done wrong.


You really should tell us why you think something is wrong. IE, do you get a compiler error? If yes, what is the error? Or does it just not do what you expect it to? If yes, then what do you expect, and what does it actually do?

Just saying "something is wrong, here is my code" means we have to sift through the entire program line by line when we don't even know what we're looking for.

So yeah -- describe the problem. For future reference. =)


That said... I noticed this:

cin >> min, max;

The comma operator does not do what you think it does. In general, the only place you should be using commas in to separate function parameters (like on lines 5, 28). Any other use is frequently error-prone by newbies... so I say just avoid it.

You probably want to do this instead:

 
cin >> min >> max;
1
2
3
// cin >> min, max;
int min, max ; // *** added
cin >> min >> max ; // *** modified 


Consider: what would happen if min is greater than max? If either or both are negative? If max - min == -1?
Ok to be more specific here:

I'm getting three different error messages which I haven't seen before, the first one being in this part of the code:
int getRandomNumber (int min, int max);
This part gives me a message saying: "candidate function not viable; no overload of 'min' matching for 'int' for 1st argument".


Secondly:
cin >> min >> max;
Even after I've fixed this part, its giving me an error message saying: "reference to overloaded function could not be resolved, did you mean to call it?


Lastly:
getRandomNumber(min, max);
This gives me an error message saying "no matching function for call to 'getRandomNumber', which leads me to wonder if I declared my function correctly.


I'm new to functions and I'm not really sure what I'm doing. All help is appreciated.
1) You did not declared min and max variables. They do not exist.

2) Your compiler refers to std::min and std::max functions which were brought into global namespace by using namespace std;. To avoid further problems and confusion either get rid of using namespace std; or do not use names like min, max, plus, distance... in your program
Ok so I've modified my code to look like this now:
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
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
using namespace std;

int getRandomNumber (int minimum, int maximum);

int main()
{
	int seedNumber, z, minimum, maximum;

	cout << "enter the seed for the random number generator: ";
	cin >> seedNumber;
	cout << endl;
	srand(seedNumber);

	cout << "enter the minimum and maximum size of the random numbers: ";
	cin >> minimum >> maximum;
	cout << endl;
	cout << endl;
	z = int getRandomNumber(int minimum, int maximum);

	cout << "random number = " << z;



}

int getRandomNumber (int minimum, int maximum)
{
	int guess;
	guess = (rand() % (maximum - minimum + 1)) + minimum;
	return guess;
}


I'm getting 1 error and zero warnings at this point, although I can still run the program but every time the random number it outputs is 0.



The error I'm getting is here:
z = int getRandomNumber(int minimum, int maximum);
The error says: "expected '(' for function style cast or type construction."


Can anyone tell me what this means? I do not see why I would need to add parentheses.
Define the local variables min and max before you try to use them.
Add int min, max ; before cin >> min >> max ;

It is not a great idea to dump using namespace std; at global scope.
However, that there happens to be std::min() and std::max() at namespace scope would not confuse the compiler; the names min and max at namespace scope would be hidden by the names min and max at local scope.

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
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
using namespace std; // **** avoid ****

int getRandomNumber (int min, int max);

int main()
{
        int seedNumber, z;

	cout << "enter the seed for the random number generator: ";
	cin >> seedNumber;
	cout << endl;
	srand(seedNumber);

	cout << "enter the minimum and maximum size of the random numbers: ";

	int min, max ; // **** added ****
	// cin >> min, max;
	cin >> min >> max ; // **** modified ****

	cout << endl;
	cout << endl;
	z = getRandomNumber(min, max);

	cout << "random number = " << z ;

        std::cout << "\n\n-----------------\n\n" ;
}

int getRandomNumber (int min, int max)
{
	int guess;
	guess = (rand() % (max - min + 1)) + min;
	return guess;
}

http://coliru.stacked-crooked.com/a/0b7a6131f3335f14
1
2
3
int function(int paramname, int foobar);  // <- how to prototype a function

int z = function( 5, 3 ); // <- how to call that function 


You are trying to call the function, but you are not calling it. Instead you are prototyping it.
This is how I do it, and it works well:

1
2
3
4
5
6
7
8
9
10
11
int randInt(int a, int b)
{

    return rand() % b + a;

}

int main()
{
    int lol = randInt(100, 1); // CREATES A RANDOM INT 100 THROUGH 1
}
Last edited on
Topic archived. No new replies allowed.