Creating randomness -Please give ideas

Hello,
I am studying C++ from a book and now I have to do some exercises-this one is to make a game that plays rock,paper scissors.Due to the fact that i am still beginner I will be happy if you find a better way for randomness than me.
I have to mention that i have covered only (constant) expressions ,operators,conversions,if,for,while,switch ,functions ,strings and vectors.
Ok, Here is the code -I will appreciate all ideas you have to make the game more Random,Simple and Efficient.
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
/* Chapet 4,ex 10 -Program that plays the game "Rock,Paper,Scissors" with the user.
Use a switch statement to solve this exercise.The program should give random answers.
For creating randomness  build a vector with a sequence of values to be used as "the next value".
Also make the user enter some values so the game wont be the same every time. */

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int randomness(int choice_of_user)//tries to make a random value using the user`s choice
{
	int i =0;
	int newchoice =0;
	vector<int>next_value(15);
	next_value[0] = 5;
	next_value[1] =-1;
	next_value[2] = 8;
	next_value[3] = 0;
	next_value[4] = 16;
	next_value[5] = 7;
	next_value[6] = 2;
	next_value[7] = 9;
	next_value[8] =-3;
	next_value[9] = 4;
	next_value[10]= 6;
	next_value[11] =-2;
	next_value[12] = 0;
	next_value[13] = -8;
	next_value[14] = 13;

	
	for( int i =0; i <next_value.size()-1;++i)
	{
		if(choice_of_user < next_value[i])
			newchoice = choice_of_user + next_value[i];
		if(choice_of_user > next_value[i])
			newchoice = choice_of_user -next_value[i];
		if(choice_of_user == next_value[i])
			newchoice = next_value[i] /2;
	}
	return newchoice;
}




void main()
{
	string users_decision ="";
	char option ='0';
	int choice_of_user =0;
	int newchoice =0;
	cout <<"Please enter a integer number from -20 to 20 followed by rock,paper or scissors." <<endl;
	while (cin >> choice_of_user >> users_decision){	
	
		newchoice = randomness(choice_of_user);
		if(newchoice <0){
		option = 'p';//option paper
		}
		if(newchoice ==0){
			option ='r';//option rock
		}
		if(newchoice >0){
			option ='s';//option scissors
		}
		switch (option){
			case 'p':
				if(users_decision == "paper")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "rock"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
			case 'r':
				if(users_decision == "rock")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "scissors"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
			case 's':
				if(users_decision == "scissors")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "paper"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
		}
	}
}
The cstlib.h includes a random function called rand. See here:

http://cplusplus.com/reference/clibrary/cstdlib/rand/

EDIT: Just saw in the program comments that you're supposed to do it with a vector of values. Maybe you could use the current time in your calculations to make it appear more randomized?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	vector<int>next_value(15);
	next_value[0] = 5;
	next_value[1] =-1;
	next_value[2] = 8;
	next_value[3] = 0;
	next_value[4] = 16;
	next_value[5] = 7;
	next_value[6] = 2;
	next_value[7] = 9;
	next_value[8] =-3;
	next_value[9] = 4;
	next_value[10]= 6;
	next_value[11] =-2;
	next_value[12] = 0;
	next_value[13] = -8;
	next_value[14] = 13;

=
1
2
3
    vector<int>next_value(15) = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
    };
Thanks pal - that will make it shorter.
but i got these mistakes:
error C2143: syntax error : missing ';' before '='
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
warning C4018: '<' : signed/unsigned mismatch

I am using Visual Studio 2008
Last edited on
this is the latest version :
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
/* Chapet 4,ex 10 -Program that plays the game "Rock,Paper,Scissors" with the user.
Use a switch statement to solve this exercise.The program should give random answers.
For creating randomness  build a vector with a sequence of values to be used as "the next value".
Also make the user enter some values so the game wont be the same every time. */

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int randomness(int choice_of_user)//tries to make a random value using the user`s choice
{
	int i =0;
	int newchoice =0;
	vector<int>next_value(15);
	next_value[0] = 5;
	next_value[1] =-1;
	next_value[2] = 8;
	next_value[3] = 0;
	next_value[4] = 16;
	next_value[5] = 7;
	next_value[6] = 2;
	next_value[7] = 9;
	next_value[8] =-3;
	next_value[9] = 4;
	next_value[10]= 6;
	next_value[11] =-2;
	next_value[12] = 0;
	next_value[13] = -8;
	next_value[14] = 13;
	
	
	for( int i =0; i <next_value.size()-1;++i)
	{
		if(choice_of_user < next_value[i])
			newchoice = choice_of_user + next_value[i];
		if(choice_of_user > next_value[i])
			newchoice = choice_of_user -next_value[i];
		if(choice_of_user == next_value[i])
			newchoice = next_value[i] /2;
	}
	return newchoice;
}
void switching(char option,string users_decision)
{
	switch (option){
			case 'p':
				if(users_decision == "paper")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "rock"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
			case 'r':
				if(users_decision == "rock")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "scissors"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
			case 's':
				if(users_decision == "scissors")
					cout <<"Nobody wins!" <<endl;
				else if(users_decision == "paper"){
					cout << "You loose!" <<endl;
				}
				else 
					cout <<"You win!" <<endl;
				break;
		}
}


void main()
{
	string users_decision ="";
	char option ='0';
	int choice_of_user =0;
	int newchoice =0;
	cout <<"Please enter a integer number from -20 to 20 followed by rock,paper or scissors." <<endl;
	while (cin >> choice_of_user >> users_decision){	
	
		newchoice = randomness(choice_of_user);
		if(newchoice <0){
		option = 'p';//option paper
		}
		if(newchoice ==0){
			option ='r';//option rock
		}
		if(newchoice >0){
			option ='s';//option scissors
		}
		switching(option,users_decision);
	}
}
chrisname wrote:
1
2
3
    vector<int>next_value(15) = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
    };

Are you using C++0x already?
Altough, it wouldn't compile in C++0x either.
This must be the stupidest programming excercise I've seen. First, using std::vector for a constant array (you neither modify size, nor contents of this array) is dumb. Second, this method of random number generation is not very smart, either. When written correctly, this program could have like 10 lines of code.

I suggest to change the book.
If you really want to create your own random function, make a function that takes a seed. A seed is generally what a random function takes to calculate some number. Now, when you use the current time as seed you will have different values every time, wich looks like random.
But I don't really know some formula that outputs pseudo-random stuff.
hunter86bg: What is the name of the book? I have to see this.
Last edited on
Are you using C++0x already?
Altough, it wouldn't compile in C++0x either.

Uhhhh... yeah.
No, I just hoped that vectors emulated arrays like USBs emulate floppies without bothering to check because I'm too lazy hardcore.
1
2
3
boost::array< int, 15 > next_value = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
};


would work, however, and provides the same interface as vector<> in terms of
element access (though of course boost::array<> is not dynamically sizable).

In lieu of boost, this is what I would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
template< typename T, size_t N >
T* abegin( T (&a)[ N ] )
{   return &a[0]; }

template< typename T, size_t N >
T* aend( T (&a)[ N ] )
{   return &a[N]; }

static const int next_value[] = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
};

std::vector<int> v( abegin( next_value ), aend( next_value ) );



Thanks for the replies guys.I am back online so I will try to answer all questions one by one.
First -this is the task:
1
2
3
4
5
6
7
/* Chapter 4,ex 10 -Write a program that plays the game "Rock,Paper,Scissors."
If you are not familiar with the game do some research(e.g., on the web using Google).
Research is a common task for programmers.Use a switch-statement to solve this exercise.
Also,the mashine should give random answers(i.e.,select the next rock,paper or scissors randomly).
Real randomness is too hard to provide just now,so just build a vector with a sequence of values to be used as"the next value."
If you build the vector into the program, it will always play the same game, so maybe you should let the user enter some values.
Try variations to make it less easy for the user to guess which move the mashine will make next.  */
Second -the book is "Bjarne Stroustrup-Programming Principles and Practice Using C++".
How smart or stupid is the book - I don`t know.About the random generation of the program - its all my idea.I know its not very smart -that`s why I am posting in www.cplusplus.com `s beginner`s forum.Third- I haven`t met anything called array in this book -so I won`t comment your post.
About this :
1
2
3
boost::array< int, 15 > next_value = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
};


would work, however, and provides the same interface as vector<> in terms of
element access (though of course boost::array<> is not dynamically sizable).

In lieu of boost, this is what I would do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

template< typename T, size_t N >
T* abegin( T (&a)[ N ] )
{   return &a[0]; }

template< typename T, size_t N >
T* aend( T (&a)[ N ] )
{   return &a[N]; }

static const int next_value[] = {
        5, -1, 8, 0, 16, 7, 2, 9, -3, 4, 6, -2, 0, -8, 13
};

std::vector<int> v( abegin( next_value ), aend( next_value ) );

I don`t know what a template ,abegin or aend is.
Last edited on
vectors but no arrays... that makes sense... not. My god some authors need to be turkey slapped.
OMG, I criticised a book written by the creator of C++:)

Well, I still think that using vector here is not a good idea, but I understand that the author decided to teach vectors before arrays. However, trying to generate random number with this strange method when you have std::rand...

@hunter86bg
First, don't look at the code posted by jsmith. It's much too early for this.

As for your random number generator. I think that the author meant something a little different. Function 'randomness' should just return successive values from the vector. So, first call to 'randomness' should return first value from the vector, second call should return second value, nth call should return last value, and (n+1)th call should return first value again. To implement this you must remember the current state between calls to 'randomness', for example by using global variables:

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
vector<int> values;
int values_index;

int initialize()
{
   values.resize(15);

   values[0] = 5;
   // Initialize rest of 'values' here...

   values_index = 0;
}

int randomness()
{
   int result = values[values_index];

   values_index++;

   // Reset index back to 0 when we reach end of the vector.
   if(values_index >= values.size())
      values_index = 0;

   return result;
}
Last edited on
My god some authors need to be turkey slapped.
You're talking about the S-man. I'm sorry, but you're gonna have to leave.

Okay, I found the book. Thankfully, the "declare a local vector and initialize it repeatedly" isn't on the book. That's just OP's work.
This looks like a pretty interesting book at first sight, though. Maybe I'll read a couple chapters.
thank you for the reply Abramus-i will think about it.I chose the book by a recommendation of a person who helped me in this forum.I checked it in google books- which is now not possible - I don`t know why,but i thought that if i can understand the writer and he is the reason C++ to exist - I guess his book is the one that will teach me most.
By the way ,helios what do you suggest me to change?
Last edited on
Helios wrote:
That's just OP's work.

That's what I figured, but I thought my comment was funny :(

Hunter86bg wrote:
By the way ,helios what do you suggest me to change?


Na, even though last time I checked I wasn't helios, I will answer that, you can't really find a book better than the guy who was the main force behind c++, well you can... some are geared towards complete beginners in software dev as well as c++, but I was only joking with my above post.

For as many good books out there, there is 10x more bad ones. So at least you know the info your reading will be correct. even if it may seem difficult to grasp at times, (I'm only speculating that someone that smart cannot be that great at writing a book geared towards nubs as well). I still haven't bought it so I probably shouldn't be speculating...
Last edited on
hunter86bg, I admit I was too fast in judging the book based on a single excercise, especially since it was inprecisely quoted. I don't know this book, but I guess that Bjarne Stroustrup is at least able to describe clearly and completely rules of C++.
I've always been of the opinion that C++ students should stick to as much of the ++ portion and as little of the C portion as possible when starting out. That includes starting off with vectors and staying away from arrays. The only real advantages arrays provide over vectors are advantages no beginning programmer should even be thinking about, and the disadvantages are just the kind of pitfalls beginners are always falling into.
In general I agree, although unfortunately sometimes "C++ way" is worse than "C way". This is one of examples - you cannot use list of initial values for a non-POD type. So instead of:

int table[] = {1,2,3};

you must write:

1
2
3
4
vector<int> table(3);
table[0] = 1;
table[1] = 2;
table[2] = 3;

(unless you use Boost Assignment Library or something similar, but it's not in the standard)
Last edited on
Topic archived. No new replies allowed.