I wrote a program that rolls a die with a user-specified number of sides. The problem is, it's too predictable.
I'm using the CodeBlocks IDE, and the compiler is GCC. The program compiles nicely as both debug and release builds, but no matter what build option I choose, the executable will return the same values every time it's run. I can't have this, because its intended use is as a tabletop RPG tool, and it would be relatively easy for a smart player to cheat if they knew the pattern by which the dice would roll.
What is the easiest way to fix this problem?
Here's the source:
#include <iostream> /* for input and output */
#include <cstdlib> /* for random numbers */
using namespace std;
void rolldie() {
cout << "How many sides to the die?" << endl << "D";
int die;
cin >> die;
int roll = rand() % die +1;
cout << endl << "The die rolled " << roll << endl << endl << "Roll another? (Y for yes, anything else for no; Capitalization counts) ";
}
int main() {
rolldie();
char again;
cin >> again;
while (again == 89) {
rolldie();
cin >> again;
}
return 0;
}