New to c++. I'd like to make a function like roll_die(unsigned int seed = ???)
where, by default the function uses srand(time(NULL))
to seed the RNG before returning a value, but also allows the user to specify a seed. Here's what I have that works
#include <iostream>
#include <ctime>
int roll_die(unsigned int seed = 0){
// Returns a random integer between 1 and 6
// time(NULL) is the number of seconds since 00:00 hours, Jan 1, 1970 UTC
// Logic
srand(seed);
int randnum = rand();
// Print what's going on
std::cout << "The random seed is: " << seed << std::endl;
std::cout << "The max random number is: " << RAND_MAX << std::endl;
std::cout << "The randomly chosen number is: " << randnum <<std::endl;
// Return the result
int result = randnum % 6 + 1;
return result;
}
This is nice, but it doesn't default to a random seed. If I try doing something like int roll_die(unsigned int seed = time(NULL)){...}
I get the warning
Unable to parse C++ default value 'time(NULL)' for argument seed of function roll_die
I assume because time(NULL)
does not return an int.
What is the proper way to do this?