I am pretty new to C++ and am trying to make a simple die roll with a Die class/main.
I can get a random number within my range 1-dieSize, however, each time I “roll the dice” it just gives me the same random number. For example, when I roll this dice three times, it will cout 111 or 222 etc instead of 3 different random rolls. Any help explaining this issue would be much appreciated!
My die header is just a basic header. My issue I’m assuming is with the random function.
main:
int main() { // Call menu to start the program Die myDie(4); cout << myDie.rollDie(); cout << myDie.rollDie(); // roll dice again cout << myDie.rollDie(); // roll again return 0; }
die.cpp:
Die::Die(int N) { //set dieSize to the value of int N this->dieSize = N; } int Die::rollDie() { // Declaration of variables int roll; int min = 1; // the min number a die can roll is 1 int max = this->dieSize; // the max value is the die size unsigned seed; seed = time(0); srand(seed); roll = rand() % (max - min + 1) + min; return roll; }
In die.cpp I have the cstdlib and ctime included.