Hello PacificAtlantic,
To cover some things that have not been said yet.
You have included "cstdlib" for "srand()" and "rand", but you have used "abs()" without including "cmath". I know that VS and its header files the "iostream" header file will eventually include "cmath", but do not count on this being the case for all compilers and header files. Implementations are different.
1 2 3
|
// Initialize functions. Define prototypes. Does not initialize anything.
int reverse(int); // <--- Changed.
int value(int); // <--- Changed.
|
In "main":
1 2 3 4 5
|
// Variables
unsigned int rng{}; // <--- Changed. "seed" would be a better name.
int val{};
int sum{};
int total{};
|
It is a good idea to initialize your variables especially "sum" and "total". Uninitialized this line
sum += total;
could produce something like this:
-858993460 = -858993460 + total
. You would just end up with a smaller negative number better known as a garbage value.
Most often the time is used to seed the RNG as
srand(static_cast<unsigned int>(time(nullptr)));
. But to use what you have it should look like:
1 2 3 4 5
|
cout << "\nPlease enter a seed for the RNG. > ";
//cout << "> ";
cin >> rng;
srand(rng);
|
"srand()" only needs done once usually near the beginning of "main". See
http://www.cplusplus.com/reference/cstdlib/srand/
https://en.cppreference.com/w/c/numeric/random
If you use "time()" to seed the RNG with "srand()" know that "time" does not return an "unsigned int".
Using "srand" and "rand" this video is helpful:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
The do/while loop does not work well. When I first tested it it only executed once. Also your code:
1 2
|
cout << "> ";
cin >> val;
|
I changed to:
1 2
|
cout << "Enter number > ";
cin >> val;
|
The wording on the prompt can be changed any way you like.
In the function "value" and in addition to what has been said there are several problems with:
return abs(reverse(num) - srand(seed) % num);
First: you are trying to return something when you have said that the function returns nothing.
return abs(reverse(num) - srand(seed) % num);
. Just the "return" would work, but is not needed at the end of a function.
Second:
return abs(reverse(num) - srand(seed) % num);
. You are trying to use a returned value of a function that returns nothing. Not sure what you will get from this, but not what you want.
Third:
return abs(reverse(num) - srand(seed) % num);
. "srand" only seeds the RNG. It returns nothing. Using it in this way does not produce any random number. I think what you wanted is
return abs(reverse(num) - rand() % num);
. Not sure what you are wanting, but this will produce a number from (0) zero to num -1, e.g., if "num" is 10 you would get 0 - 9. If you want 1 - 10 you would have to add 1
rand() % num + 1
.
Andy