Not repeating, not generating random numbers...

alright, so im making a dice game, and this is my code. i have 2 problems. first of all, its not repeating it five times. Second of all, my random numbers stay the same, every time i try to run it... I know i have bad programming grammar, and i have no comments, but i run it fine, no errors. Could anyone please help me, as i am at a loss now...
Any input would be great!
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{ string Start;
    cout << "Welcome to the dice game!" << endl;
    cout     << " " <<endl;
     cout    << "The rules are simple! you will roll two die, and the computer will roll two die." << endl;
     cout    << "The two die get added up, and the person with the" << endl;
     cout    << "most points get a point. at the end of five rounds." << endl;
     cout    << "The person with the highest score wins!" << endl;
     cout << "Enter 'Start'(case sensetive) to start the game!" << endl;
     cin >> Start;
    string StartCheck = "Start";
    int PlayerCount = 0;
    int CompCount = 0;
    int TimeCount = 5;
    while(TimeCount > 0)
    {
        TimeCount- 1;
    if( Start == StartCheck)
     {int rN1 = rand() % 101;
     int rN2 = rand() % 101;
         int RollDie1;
         int RollDie2;
         cout << "Lets go!" << endl;
         RollDie1 = rN1;
         RollDie2 = rN2;
         cout << "Player rolled "<< RollDie1 <<", and  " << RollDie2 <<"." << endl;
        int DieTotal = RollDie1 + RollDie2;
         cout << "Your total is " << DieTotal << "!" << endl;
         cout << "Now, the Computer shall roll its die!" << endl;
         int cRdie = rand() %101;
         int cRdie2 = rand() % 101;
         cout << "The computer rolled " << cRdie<< ", and " << cRdie2<< "!" << endl;
      int   cRdieTotal = cRdie2 + cRdie;
      cout << "The computers total is " << cRdieTotal << endl;
      if (cRdieTotal > DieTotal)
       {
           cout << "The computer won that round!" << endl;
           CompCount+ 1;
       }
       else if (cRdieTotal < DieTotal)
       {
           cout << "Wow! you won that round!" << endl;
           PlayerCount++;
           cout << PlayerCount << " is your score!" << endl;
           cout << CompCount << " is the computers score!" << endl;
     }
    return 0;
}
    }
}
return 0; ends the program so it should be outside the loop
you need to seed rand with srand() to get random numbers
i Do that, but then it says too few arguments for srand(). how do i use it, and do i need an #include for it?
well, i should state my problem better. i get the random numbers i want, but everytime i run the thing, it has the same random numbers...
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
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{ string Start;
    cout << "Welcome to the dice game!" << endl;
    cout     << " " <<endl;
     cout    << "The rules are simple! you will roll two die, and the computer will roll two die." << endl;
     cout    << "The two die get added up, and the person with the" << endl;
     cout    << "most points get a point. At the end of five rounds," << endl;
     cout    << "the person with the highest score wins!" << endl;
     cout << "Enter 'Start'(case sensitive) to start the game!" << endl;
     cin >> Start;
    string StartCheck = "Start";
    int PlayerCount = 0;
    int CompCount = 0;
    int TimeCount = 5;
    while(TimeCount > 0)
    {

    if( Start == StartCheck)
     {int rN1 = rand() % 7;
     int rN2 = rand() % 7;
         int RollDie1;
         int RollDie2;
         cout << "Lets go!" << endl;
         RollDie1 = rN1;
         RollDie2 = rN2;
         cout << "Player rolled "<< RollDie1 <<", and  " << RollDie2 <<"." << endl;
        int DieTotal = RollDie1 + RollDie2;
         cout << "Your total is " << DieTotal << "!" << endl;
           cout << " " << endl;
         cout << "Click for the computers roll!" << endl;
         system("PAUSE");
         cout << "Now, the Computer shall roll its die!" << endl;
           cout << " " << endl;
         int cRdie = rand() % 7;
         int cRdie2 = rand() % 7;
         cout << "The computer rolled " << cRdie<< ", and " << cRdie2<< "!" << endl;
           cout << " " << endl;
      int   cRdieTotal = cRdie2 + cRdie;
      cout << "The computers total is " << cRdieTotal << endl;
        cout << " " << endl;
      if (cRdieTotal > DieTotal)
       {
           cout << "The computer won that round!" << endl;
           CompCount++ ;
 cout << " " << endl;
           cout << " " << endl;
           cout << "------------------" <<endl;
           cout << "|" << PlayerCount << " is your score!|" << endl;
             cout << "------------------" <<endl;
             cout << " " << endl;
           cout << "---------------------------" <<endl;
           cout << "|" << CompCount << " is the computers score!|" << endl;
           cout << "---------------------------" <<endl;
            cout << " " << endl;
             cout << TimeCount << " Rounds remain!" << endl;
             cout << " " << endl;
           cout << "Click any button to go to next round!" << endl;
            TimeCount--;
           system("PAUSE");
       }
       else if (cRdieTotal < DieTotal)
       {
           cout << "Wow! you won that round!" << endl;
           PlayerCount++;
           cout << " " << endl;
           cout << "------------------" <<endl;
           cout << "|" << PlayerCount << " is your score!|" << endl;
           cout << "------------------" <<endl;
           cout << " " << endl;
           cout << "---------------------------" <<endl;
           cout << "|" << CompCount << " is the computers score!|" << endl;
           cout << "---------------------------" <<endl;
             cout << " " << endl;
             cout << TimeCount << " Rounds remain!" << endl;
             cout << " " << endl;
           cout << "Click any button to go to next round!" << endl;
            TimeCount--;
           system("PAUSE");
     }
     }
    }


if (CompCount > PlayerCount)
{  cout << " " << endl;
    cout << "Sorry, but the computer won." << endl;
      cout << " " << endl;
}
else if(CompCount < PlayerCount)
{  cout << " " << endl;
    cout << "Woo Hoo! you one the game!" << endl;

}else if(CompCount == PlayerCount)

{cout << "Well, it was a tie. Simple as that." << endl;
cout << " " << endl;
}
}

is my final draft, but put it into a compiler, run it a few times. the output of random values are always the same, so the ending is always the same...
I'll try to explain rand / srand for you. When dealing with computers there is no
such thing as random numbers. The rand function just takes a base number runs it
through a complex algorithm and the result appears to be random but if you
new the base number and the algorithm you could predict the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	cout << "Random numbers: ";
	
	int random = 0;

	for (int i = 0; i < 5; i++)
	{
		random = rand();
		cout << random << ',' << ' ';
	}

	cin.ignore();
	return 0;
}


run this program a couple of times and you will see that you always get the same numbers
this is because the base number and the algorithm are the same.

This is where srand() comes in. srand allows us to change the base number.
this in turn gives us differant results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
using namespace std;


int main()
{
	srand(0); //<<-------------------------change this number
	int random = 0;

		cout << "Random numbers #: ";

		for (int i = 0; i < 5; i++)
		{

			random = rand();
			cout << random << ',' << ' ';
		}

	cin.ignore();
	return 0;
}


run this program a couple of times change the base number after each run you will
see that you get differant results each time.


Unfortunatly for every base number you will get the same results every time you
run the program. So to get differant results you need to keep changing the base number.
Usually this is done by using this statement srand(time(NULL)); Honestly i forget
what time(NULL) does exactly, but it evaluates to a number and this number is based
on time. Since time is constantly changing the number it evaluates to is also constantly changing.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Example
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main()
{
	srand(time(NULL)); 
	int random = 0;

		cout << "Random numbers #: ";

		for (int i = 0; i < 5; i++)
		{

			random = rand();
			cout << random << ',' << ' ';
		}

	cin.ignore();
	return 0;
}



try running this code and you will see what I mean.

i Do that, but then it says too few arguments for srand(). how do i use it, and do i need an #include for it?

srand takes an unsigned integer and is in the <cstdlib> header file
time is in the <ctime> header file
Last edited on
Topic archived. No new replies allowed.