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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <time.h>
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 << " <R or H>?" << endl;
			cin >> rollOrHold;
			
		}
	}
	while (rollOrHold == 'h')
	{
		humanTotalScore += currentScore;
		break;
		
	}
Call srand only once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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().
1
2
3
4
5
6
7
8
9
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);
Assigment instead of comparison(==)
Tried, but still not working. Every time I look over it I can't find any errors.
The part of the code that alters continuePlay is only performed once before the loop.
Topic archived. No new replies allowed.