### Random number generator problems

I am working on a console version of the dice game pig. So there are two players, the computer and you. You either choose to roll a random number or hold. If you get a 1 then you lose all your current score and its the computers turn.

I have most everything working, but the computer always gets the same number for some reason. So if it gets a 4 the first time it will get a 4 every time till it holds. Also, if I get a 1 then the computer will always get a 1 after that.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include "stdafx.h" #include #include #include #include #include using namespace std; const int scoreLimit = 100; int diceRoll(); int humanTurn(int& humanTotalScore); int computerTurn(int& computerTotalScore); int diceRoll() { int x; srand(time(0)); int const j = 1, k =6; x = (j+(rand()%(k-j+1))); return x; } int _tmain(int argc, _TCHAR* argv[]) { bool continuePlay = 1; int humanTotalScore = 0, computerTotalScore = 0; cout << setw(25)<< right << "GAME OF PIG" << endl; cout << "=============================================" << endl; if((humanTotalScore < scoreLimit) && (computerTotalScore < scoreLimit)) { continuePlay = 1; } else { continuePlay = 0; } do { cout << "Computers Score: " << computerTotalScore << endl; humanTurn(humanTotalScore); computerTurn(computerTotalScore); } while(continuePlay = 1); if(continuePlay = 0, humanTotalScore > computerTotalScore) { cout << "Good job! You won."; } else { cout << "Sorry, you lost. Try again!"; } getchar(); getchar(); return 0; } int humanTurn(int& humanTotalScore) { int currentScore = 0; int lastRoll; char rollOrHold; cout << "Your total score is: " << humanTotalScore << "." << endl; cout << "Press r to roll or h to hold." << endl; cin >> rollOrHold; while (rollOrHold == 'r') { lastRoll = diceRoll(); if (lastRoll == 1) { cout << "You rolled a 1, ending your turn." << endl; break; } else { currentScore += lastRoll; cout << "You Rolled a " << lastRoll << ". Your Current Score is: " << currentScore << " ?" << endl; cin >> rollOrHold; } } while (rollOrHold == 'h') { humanTotalScore += currentScore; break; }``````
Call srand only once:
 ``1234567891011121314`` ``````int diceRoll() { int x; //srand(time(0)); int const j = 1, k =6; x = (j+(rand()%(k-j+1))); return x; } int _tmain(...) { srand(time(0)); ...``````
Don't re-seed random with every call to diceRoll. Just seed it once at the beginning of main().
 ``123456789`` ``````int diceRoll() { int x; srand(time(0)); //Move this to the beginning of main. int const j = 1, k =6; x = (j+(rand()%(k-j+1))); return x; }``````
Thanks guys! It seems to be working. But when I reach a 100 the game just keeps going but its supposed to end and say you won, or that you lost.
`while(continuePlay = 1);`
The part of the code that alters `continuePlay` is only performed once before the loop.