How Do I Reset Integers?

I'm very new to C++ and I have been watching tutorials and reading about it. After learning about random numbers in C++ I decided to make a simple rock, paper, scissors program. A few hours later and it's done, at the end it asks if the user wants to play again, and it starts over, but the only problem is it's using the same number from the last round, thus putting out the same thing each time. So, is there a way I can reset my int x?

Thanks!
closed account (10oTURfi)
srand(time(NULL)) ??
Last edited on
Yes, you're going to want to #include <ctime> and use the function time.
The statement
int randomNumber = rand() + time(0);
will generate a new number for the variable randomNumber every time the program is run.
(You can use the modulus operator if you want randomNumber to be in a certain domain...).
Last edited on
Aaaaaa!
1
2
3
//BAD: int randomNumber = rand() + time(0);
srand(time(NULL)); //NULL == 0 (or 0l)
int randomNumber = rand(); //or "rand() % N" if on [0,N) 

What code are you using to determine the computers choice (play, rock|paper|scissor) BusinessSloth?
Last edited on
Just curious, why is
int randomNumber = rand() + time(0);
bad?
rand() % time() is mostly just really weird. Though I suppose it will produce the desired effect... kinda.

I wouldn't recommend doing it because it doesn't change the stream of numbers produced by rand, it only offsets them. Which makes the numbers more predictable than properly seeding.

For example if rand normally gives you the following string:
15
62
85
1

Then rand + time might give you this string
20
67
90
6

(same pattern, just +5)

Whereas if you properly seed, you'd get a completely different pattern (sorta -- technically you get the same pattern just at a different point in the sequence so it appears to be different).
Exactly, the pattern will appear to be different, but really it would completely depend on how fast your program ran. Plus you don't want to be checking the time 50 times if you only need to check it once.

Edit: Disch, I think you meant "rand() + time()" not "rand() % time()" as that would be really weird... Hmm...
Last edited on
Thanks for all the replies guys! Sorry, I should have been more specific when I was explaining my problem, it is random every time I run it, close it, and then run it again, but I have a do loop going back to the beginning so you don't have to shut it down for a different outcome. I have what you guys are talking about, I think. Here's my code so you can see it. I know this is most likely very poorly done haha, but here it is.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    int rock = 1;
    int paper = 2;
    int scissors = 3;
    int a; //user answer
    int x = 1+(rand()%3);// computer answer
    int p; // play again

    do{
    cout << "Welcome to Rock, Paper, Scissors Simulator\nPress enter to play\n";
    cin.get();

    cout << "Press\n 1. Rock\n 2. Paper\n 3. Scissor\n \n ";

    do{
    cin >> a;

    if (a == 1){
    cout << "\nYou chose Rock\n";
    break;
    }
    if (a == 2){
    cout << "\nYou chose Paper\n";
    break;
    }
    if (a == 3){
    cout << "\nYou chose Scissors\n";
    break;
    }
    else {
    cout << "\nThat was not an option! Choose again\n \nPress\n 1. Rock\n 2. Paper\n 3. Scissor\n";
    cin.get();
    }

    }
    while ( a != 1,2,3);

     if (x == 1){
    cout << "\nThe computer chose Rock\n";
    cin.get();

    }
    if (x == 2){
    cout << "\nThe computer chose Paper\n";
    cin.get();

    }
    if (x == 3){
    cout << "\nThe computer chose Scissors\n";
    cin.get();

    }

    cout << "\nDo you want to play again?\n\nPress\n 1. Yes\n 2. No\n";
    cin >> p;
    cout << "\n\n\n";

    if (p == 1)


    if (p == 2){
        cout << "Thanks for playing!";
    break;}


    }
    while ( p == 1);


}


Also any tips on how this could be improved would be greatly appreciated! Thanks
Last edited on
awesome script. so awesome in fact that i couldnt help but make it myself. i am also a very fresh beginner and so when it says

1>LINK : fatal error LNK1168: cannot open c:\documents and settings\my documents\visual studio 2010\Projects\rockpaperscissors\Debug\rockpaperscissors.exe for writing

i have no idea what to do. can anyone out there in cyberspace help?
C++ isn't a scripting language. Anyways: That either means you currently have the executable running right now, or you generally don't have write permissions for that directory.
Make sure you include
#include <StdAfx.h>
if you're using Visual C++

Just a thought
Last edited on
sorry about the bad lingo. like i said, im very new. yeah i have <stdafx.h> going and i am on visual. was this written on visual?
got it. for my other super simple testing programs i havent needed to capitalize the 'S' and 'A' in stdafx but that did it. thanks a bunch
No, it was written in CodeBlocks and no problem!
Last edited on
haha ok so now im trying to add to your program by specifying whether you win or lose. i would figure this would be a simple addition of a few more if statements like:
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
//win or lose

		if (p == 1 && c == 3){
			cout << "\nyou win!\n";
		}

		if (p == 2 && c == 1){
			cout << "\nyou win!\n";
		}

		if (p == 3 && c == 2){
			cout << "\nyou win!\n";
		}

		if (p == 1 && c == 2 ){
			cout << "\nyou lose\n";
		}

		if (p == 2 && c == 3){
			cout << "\nyou lose\n";
		}

		if (p == 3 && c == 1){
			cout <<"\nyou lose\n";
		}


yet somehow this does not work. it simply doesnt show anything as changed
im lying this works beautifully :) enjoy the improvement. or not. ha thanks for the original though
I'm very new myself but was able to fix some issues and streamline abit:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

	int a;		// user answer
	int x;		// computer answer
	int p;		// play again

	do
	{
		srand(time(0));
		x = rand()%3 + 1;
		
		cout << "\n~ Welcome to Rock, Paper, Scissors Simulator ~\n"
			<< "\n\t\tPress enter to play\n";
		cin.get();
				
		do
		{
			cout << "Choose:\n\n 1. Rock\n 2. Paper\n 3. Scissor\n \n ";
			cin >> a;

			switch (a)
			{
				case 1:
					cout << "\nYou chose Rock\n";
					break;
				case 2:
					cout << "\nYou chose Paper\n";
					break;
				case 3:
					cout << "\nYou chose Scissors\n";
					break;
				default:
					cout << "\nThat was not an option!\n" << endl;
			}

			
		}
		while (a < 1 || a > 3);


		switch (x)
		{
			case 1:
				cout << "\nThe computer chose Rock\n";
				break;
			case 2:
				cout << "\nThe computer chose Paper\n";
				break;
			case 3:
				cout << "\nThe computer chose Scissors\n";
		}


		if (a == x)
		{
			cout << "\nIt's a tie!\n";
		}
		else if ((a == 1 && x == 2) || (a==2 && x == 3) || (a==3 && x == 1))
		{
			cout << "\nYou lose!\n";
		}
		else
		{
			cout << "\nYou win!\n";
		}

			
		cout << "\nDo you want to play again?\n\n 1. Yes\n 2. No\n";
		cin >> p;

		cout << "\n\n\n";
					
				
	}
	while (p == 1);
	
	return 0;
}
Okay, here's the last update before sleepy time:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{

	int a, x, p;		// user answer, computer answer, play again
	string choice[3] = {"rock", "paper", "scissors"};

	do
	{
		srand(time(0));
		x = rand()%3 + 1;
		
		cout << "\n~ Welcome to Rock, Paper, Scissors Simulator ~\n\n";
				
		do
		{
			cout << "Choose:\n\n 1. Rock\n 2. Paper\n 3. Scissor\n \n ";
			cin >> a;			
		}
		while (a < 1 || a > 3);

		cout << "\nYou chose " << choice[a - 1] << "."
		<< "\nThe computer chose " << choice[x - 1] << "." << endl;

		if (a == x)
			cout << "\nIt's a tie!";
		else if ((a == 1 && x == 2) || (a==2 && x == 3) || (a==3 && x == 1))
			cout << "\nYou lose because " << choice[x - 1] << " beats "
			<< choice [a - 1] << ".";
		else
			cout << "\nYou win because " << choice[a - 1] << " beats "
			<< choice [x - 1] << ".";
	
		cout << "\n\nDo you want to play again?\n\n 1. Yes\n 2. No\n";
		cin >> p;
				
	}
	while (p == 1);

	cout << "Goodbye." << endl;
	return 0;
}


* readies bedtime bear for the sandman *
Nice code carebearboy, I also see something in there that solved my problem, thanks!
I'm pretty sure your problem (or one of them at least) was with while ( a != 1,2,3); //line 44 .
You wanted while( a != 0 || a != 2 || a != 3 ); //or "a < 1 || a > 3" like above .
Are you still having problems with your program?
Topic archived. No new replies allowed.