Hi, I'm trying to make a program to play the game Craps. The rules of the game are working fine, but whenever I run the program, it generates the same random numbers EVERY time.
Code:#include <iostream> #include <cstdlib> #include <time.h> using namespace std; int roll(); int round = 1; int die1; int die2; int total; int point; int rollNumber; int numberWon = 0; int numberLost = 0; bool roundWin; bool roundOver; //BEGIN MAIN int main() { while(round <= 10) { roundWin = false; roundOver = false; roll(); if(total == 7 || total == 11) { roundWin = true; roundOver = true; cout<<"WIN!"<<endl; } else if(total == 2 || total == 3) { roundOver = true; cout<<"LOSE!"<<endl; } else { point = total; } if(roundOver != true) { do { roll(); } while (total != 7 && total != point); if(total == point) { roundWin = true; roundOver = true; \ cout<<"WIN!"<<endl; } else if(total == 7) { roundOver = true; cout<<"LOSE!"<<endl; } } if(roundWin == true) numberWon++; else if(roundWin == false) numberLost++; round++; } cout<<"OVERALL: "<<endl; cout<<numberWon << " wins and " << numberLost << " losses" <<endl; system("PAUSE"); return 0; } int roll() { time_t seconds; time(&seconds); srand((unsigned int) seconds); die1 = (rand() % 6) + 1; die2 = (rand() % 6) + 1; total = die1 + die2; rollNumber++; cout<<die1 << " " << die2<<endl; }
Some unused variables are up there, I know. I'm mainly just concerned with the roll() function because it keeps giving me the same numbers for all rolls. I tried doing the following instead --
This gave me 10 different rolls, but everytime I ran the program it still gave me the same results. How can I create new random numbers everytime I call the roll() function?Code:srand(rand());