Having random issues

I wanted to get some general practice in C++ so I decided to try to make the game Rock Paper Scissors. The following is the first part of my code. The rand always chooses 3 (scissors). I apologize for the poor formatting, this is my first post.


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
int a,b,c,d,g;
a=0;
b=1;
c=2;
g=1;
//ignore the menu
cout<<"Start menu"<<endl<<"0: Start"<<endl<<"1: Highscore"<<endl<<"2: Exit"<<endl;
cin>>d;
if(d==0)
{

int rock,paper,scissors,e,f;
rock=1;
paper=2;
scissors=3;

srand( time(NULL) );
f=rand () % 3+1;

cout<<"Welcome to Rock Paper Scissors"<<endl<<"Please enter your name: ";
char name[100];
cin>>name;
cout<<"Hello "<<name<<endl;
while(g==1)
{
cout<<"please select rock, paper, or scissors from the following key"<<endl<<"rock: 1"<<endl<<"paper: 2"<<endl<<"scissors: 3"<<endl;
cin>>e;

if(f==e)
{
cout<<"It's a tie!"<<endl;
cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
cin>>g;
if(g==0)
{
exit(1);
}

}
if(f!=e)
{
if(f==1&&e==2)
{
cout<<name<<" chose paper"<<endl<<"The Computer chose rock"<<endl;
cout<<name<<" wins! :)"<<endl;
cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
cin>>g;
if(g==0)
{
exit(1);
}

And so it goes on for if(f==1&&e==3) etc...
You might be simply getting unlucky or the problem is elsewhere as the output from running the srand/rand functions as you set up is producing correct results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;
	
int main()
{
	int f;

	srand( time(NULL) );

	for( int i = 0; i < 100; i++ )
		cout << rand () % 3+1 << " ";

	cin.get();
}



3 1 3 2 3 1 3 3 3 2 3 2 1 2 1 3 1 2 3 1 2 1 3 2 1 3 3 2 2 2 1 3 3 1 2 2 1 2 2 3
2 2 3 3 1 2 2 3 1 3 3 2 2 3 3 1 3 2 3 1 1 2 1 1 3 3 3 1 3 2 2 1 3 2 3 1 3 3 1 1
3 1 3 1 2 3 1 1 2 2 2 2 2 2 3 2 1 3 3 1


Try using code tags to present your source in a more readable format and post it all.
Last edited on
Thanks for the help, I am a beginner at using forums as well as C++. Here is the whole code in a more readable format. Also I tested it about 20 times with every int f=3
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
	int a,b,c,d,g;
	a=0;
	b=1;
	c=2;
	g=1;
//ignore the menu
	cout<<"Start menu"<<endl<<"0: Start"<<endl<<"1: Highscore"<<endl<<"2: Exit"<<endl;
	cin>>d;
	if(d==0)
	{

		int rock,paper,scissors,e,f;
		rock=1;
		paper=2;
		scissors=3;

		srand( time(NULL) );
		f=rand () % 3+1;

		cout<<"Welcome to Rock Paper Scissors"<<endl<<"Please enter your name:  ";
		char name[200];
		cin>>name;
		cout<<"Hello "<<name<<endl;
		while(g==1)
		{
		cout<<"please select rock, paper, or scissors from the following key"<<endl<<"rock: 1"<<endl<<"paper: 2"<<endl<<"scissors: 3"<<endl;
		cin>>e;
		
		if(f==e)
		{
			cout<<"It's a tie!"<<endl;
			cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

		}
		if(f!=e)
		{
			if(f==1&&e==2)
			{
				cout<<name<<" chose paper"<<endl<<"The Computer chose rock"<<endl;
				cout<<name<<" wins! :)"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

			}
			if(f==1&&e==3)
			{
				cout<<name<<" chose scissors"<<endl<<"The Computer chose rock"<<endl;
				cout<<name<<" loses :("<<endl<<"Better luck next time"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

			}
			if(f==2&&e==1)
			{
				cout<<name<<" chose rock"<<endl<<"The Computer chose paper"<<endl;
				cout<<name<<" loses :("<<endl<<"Better luck next time"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

			}
			if(f==2&&e==3)
			{
				cout<<name<<" chose scissors"<<endl<<"The Computer chose paper"<<endl;
				cout<<name<<" wins! :)"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

			}
			if(f==3&&e==1)
			{
				cout<<name<<" chose rock"<<endl<<"The Computer chose scissors"<<endl;
				cout<<name<<" wins! :)"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}


			}
			if(f==3&&e==2)
			{
				cout<<name<<" chose paper"<<endl<<"The Computer chose scissors"<<endl;
				cout<<name<<" loses :("<<endl<<"Better luck next time"<<endl;
				cout<<"Would you like to try again?"<<endl<<"Yes: 1"<<endl<<"No: 0"<<endl;
			cin>>g;
			if(g==0)
			{
				exit(1);
			}

			}
		}

	
	if(d==1)
	{
		exit(1);
	}
	if(d==2)
	{
		exit(1);
	}
	}

	}

return 0;

}
Last edited on
It's really hard to understand what your code is doing because all your variable names are poor. 'a', 'b', 'c', etc don't really tell me what those variables are for.

From what I gather, 'e' is the user r/p/s pick and 'f' is the computer pick, right? Better names for those vars would have been 'userpick' and 'computerpick' respectively. Get in the habit of picking descriptive names.

Anyway the computer is picking the same thing every time because you only have it pick a choice once, outside the loop. So when the user plays again, the computer never changes its choice. You want to move the computer's choice inside the loop so that it gets chosen again with a new random number each time the game is played.

Also, in order to get a different stream of random numbers every time you run the program, you need to seed the random number generator. This is done with the srand() function. Do this once and only once at the start of your program:

 
srand( (unsigned)time(0) );


(do not do it before every call to rand -- only do it once)
Last edited on
Sorry about that, I'm terrible about naming my variables well. You are correct about var e and f. I put it inside the while loop and it works fine now. Thanks so much for the help! I apologize, but I'm slightly confused by the second half of your reply. This is mostly because I only have a fragile grasp on the rand function in general. Do I need user input to seed the random number generator? e.g.
1
2
srand( (unsigned)time(x) );
cin>>x;


Thanks again.
No. Just copy my line as posted and paste it at the start of main(). That's it.

If you want to know how it works.....



Random number generators are basically just mathematical formulas. A very simple RNG might look something like this:

 
output = (previous_output * X) + Y;


Where 'X' and 'Y' are constants chosen to produce "randomness" (typically they're prime numbers).

say for example you have a 32-bit RNG with the above formula, where X=69069 and Y=362437

Starting with 'previous_output = 0', that gives you the below string of "random" numbers:

(printed in hex)


000587C5
D41D4186
994CCE13
771083FC
B2EF0491
71388CE2
1657D2BF
2CCBFDB8
38FA0C1D
5EFBB3FE
...


What srand does, is it simply tells the RNG where to start. That is, it gives it the first 'previous_output' value. If you pass srand the same number, you will get the same string of random numbers every time. In order to get different strings, you need to give it a different seed. By giving it time(), we're just using the current time as the seed... since the user won't run the program more than once at the exact same time -- every time he runs it time() will be different, so you have a different seed, which starts the RNG in a different state, which produces different random numbers.


To help illustrate further, here's one way to mimic rand/srand:

1
2
3
4
5
6
7
8
9
10
11
12
unsigned rng_state = 0;

void srand(unsigned seed)
{
    rng_state = seed;
}

unsigned rand()
{
    rng_state = (rng_state * 69069) + 362437;
    return rng_state;
}
Topic archived. No new replies allowed.