Random Number Generator issues

It seems as though my program is working great but I'm having problems with my random number generator. It keeps giving me a return value from the computer of scissors (or 3) for the computers random choice. What can I do?

Thanks,
goldyn
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
//Type of Assignment: Homework Assignment 5
//Problem Number: 5
//Section Number: 100
//Author: Nate Kimpler
//Date Assigned: 10/18/2014
//Program Name: Assignment5
//File Name: Assignment5.cpp
//Time Spent on Program: 

//Purpose of Program: This program is meant to
//prompt the user to choose between 4 menu options.
// Each option will be the users choice of rock, paper, or scissors.
// the computer will then randomly generate a number and pit you against the computer




#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void user(int getUserChoice);
void comp(int getComputerChoice);
void winner(int UserChoice, int ComputerChoice);
	

int main()
{
int getComputerChoice = rand() % 3 + 1; //generates random number out of 3
int getUserChoice;


	do
	{
		cout << "Game Menu" << endl;
		cout << "---------" << endl;
		cout << "1) Rock" << endl;
		cout << "2) Paper" << endl;
		cout << "3) Scissors" << endl;
		cout << "4) Quit Program" << endl;
		cout << "Enter your choice(1-4)" << endl;
		cout << endl;
		cin >> getUserChoice; //takes user choice

		//validates menu selection
		while (getUserChoice < 1 || getUserChoice > 4)
		{
			cout << "That was not an option. Please enter a valid choice from the menu: ";
			cin >> getUserChoice;
			cout << endl;
		}
	user(getUserChoice);
	comp(getComputerChoice);
	winner(getUserChoice, getComputerChoice);

	}
	while (getUserChoice != 4);
	
		return 0;
	
}

void user(int getUserChoice)
{
	if (getUserChoice == 1)
		cout << "You picked Rock." << endl;
	else if (getUserChoice == 2)
		cout << "You picked Paper." << endl;
	else if (getUserChoice == 3)
		cout << "You picked Scissors." << endl;
	else if (getUserChoice == 4)
		return;
}
void comp(int getComputerChoice)
{
	if (getComputerChoice == 1)
		cout << "The Computer picked Rock." << endl;
	else if (getComputerChoice == 2)
		cout << "The Computer picked Paper." << endl;
	else if (getComputerChoice == 3)
		cout << "The Computer picked Scissors." << endl;
		cout << endl;
}
	
void winner(int UserChoice, int ComputerChoice)
	{
	if (UserChoice == 1)
	{
		if (ComputerChoice == 1)
			cout << "It's a tie, no one wins!" << endl;
		else if (ComputerChoice == 2)
			cout << "You lose." << endl;
		else if (ComputerChoice == 3)
			cout << "You Win!" << endl;
	}
	else if (UserChoice == 2)
	{
		if (ComputerChoice == 2)
			cout << "It's a tie, no one wins!" << endl;
		else if (ComputerChoice == 3)
			cout << "You lose." << endl;
		else if (ComputerChoice == 1)
			cout << "You Win!" << endl;
	}
	else if (UserChoice == 3)
	{
		if (ComputerChoice == 3)
			cout << "It's a tie, no one wins!" << endl;
		else if (ComputerChoice == 1)
			cout << "You lose." << endl;
		else if (ComputerChoice == 2)
			cout << "You Win!" << endl;
	}
	
	cout << "Thanks for playing! Please play again!" << endl;
	cout << endl;
}

Last edited on
The algorithm used by rand function will give you random numbers, but always the same.
Use current time as seed to always get different random number:

1
2
srand(time(0)); // <-- altering the algorithm
rand();


Also you should assign the value to getComputerChoice in your comp function (use a pointer), otherwise you will have get a random number, but it will be the same one as long as your program is running.

And a tip: use switch instead of if else ;)
Last edited on
Topic archived. No new replies allowed.