I'm having some difficulties with the program. The problem is that each time the program is run (meaning within the while loop) the same numbers are generated. I'm not quite sure why. I've done some other Google searching and common issues were people using rand() more than once or inside of a loop, with not seeding the generator in a close second. the call for rand() is well before the loop and only done once. I've also used the standard method of seeding with the time(0). Not quite sure where I need to go to make each time through a different set of numbers be generated.
*side note while this is an assignment, the loop is not a requirement of the assignment, I just have to generate a random set of numbers (like in a dice game).
*side note while this is an assignment, the loop is not a requirement of the assignment, I just have to generate a random set of numbers (like in a dice game).
#include <iostream> #include <ctime> #include <string> using namespace std; int main() { srand(static_cast<unsigned int>(time(0))); int randomNumber = rand(); int die1 =(randomNumber % 6) + 1; int die2 =(randomNumber % 3) + 3; int die3 =(randomNumber % 4) + 2; int die4 =(randomNumber % 6) + 1; int die5 =(randomNumber % 2) + 4; string answer; cout << " ***** " << " " << " ***** " << " " << " ***** " << " " << " ***** " << " " << " ***** " << endl; //top of dice 1-5 cout << " * * " << " " << " * * " << " " << " * * " << " " <<" * * " << " " << " * * " << endl; cout << " * " << die1 << " * " <</*die 1 */ " " << " * " << die2<< " * " /* die 2 */ << " " << " * " << die3 << " * " <</* die 3 */ " " << " * " << die4 << " * " <</* die 4 */ " " << " * " << die5 << " * " <</* die 5 */ endl; cout << " * * " << " " << " * * " << " " << " * * " << " " << " * * " << " " << " * * " << endl; cout << " ***** " << " " << " ***** " << " " << " ***** " << " " <<" ***** " << " " << " ***** " << endl; //bottom of dice 1-5 cout << "Do you wish to reroll the dice?" << endl; cin >> answer; while (answer =="y") { cout << " ***** " << " " << " ***** " << " " << " ***** " << " " << " ***** " << " " << " ***** " << endl; //top of dice 1-5 cout << " * * " << " " << " * * " << " " << " * * " << " " <<" * * " << " " << " * * " << endl; cout << " * " << die1 << " * " <</*die 1 */ " " << " * " << die2<< " * " /* die 2 */ << " " << " * " << die3 << " * " <</* die 3 */ " " << " * " << die4 << " * " <</* die 4 */ " " << " * " << die5 << " * " <</* die 5 */ endl; cout << " * * " << " " << " * * " << " " << " * * " << " " << " * * " << " " << " * * " << endl; cout << " ***** " << " " << " ***** " << " " << " ***** " << " " <<" ***** " << " " << " ***** " << endl; //bottom of dice 1-5} cout << "Do you wish to reroll the dice?" << endl; cin >> answer; } system("PAUSE"); return 0; }