How to randomize a number between 1 and 2

closed account (LyUoGNh0)
Hi. I'm making a text adventure game and I was wondering how I could randomize a number. (BELOW IS A PART OF MY CODE)
Score is declared earlier in the code. I want right_Way to randomize a number between 1 or 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// The Which way to go loop
while (game_Is_Running == 1) {
    // generate the right way
    right_Way = 2; // I want right_Way to equal 1 or 2

    cout << "You come across a two-way intersection. You can go left (1) or right (2)" << endl;
    cout << "Which way do you want to go?" << endl;	
    cin >> user_Input;
						
    if (user_Input == right_Way) {
        cout << "Congrates! You picked the right way!" << endl;
        score = score + 1;
        cout << "Your score is now " << score << endl;
	system("pause");
	system("cls");
    }else {
	cout << "You picked the wrong way..." << endl;
	score = score - 1;
	cout << "Your score is now " << score << endl;
	system("pause");
	system("cls");

    }		
}
In the beginning of your code, before the while loop type:
 
srand(time(0));

This will set the seed for your randomization.

Then you will need to include two things;
1
2
#include <time.h>
#include "windows.h" 

time.h This will allow you to get the time to your srand().
windows.h This will allow you to use srand() and rand().

Then you set right_Way to:
 
right_Way = (rand() % 2) + 1;


And there you have it, right_Way is now the value 1 or 2.
Hi. You should #include <ctime>
Then:
srand(time(0)) , so the number will be random every time.
1
2
3
4
5
6
int right_Way = rand() % 2 + 1;
if (right_Way == user_Input) { //If random number = input, your'e lucky (50% chance);
        cout << "Congrates! You picked the right way!" << endl;
        score = score + 1;
        ....
}


I think you want it to be like that.
Last edited on
closed account (LyUoGNh0)
2 errors, 1 warning. Error1: missing type specifier - int assumed. Note: C++ does not support default-int. Error2: 'srand' : redefinition; previous definition was 'function' Warning: 'initializing' : conversion from 'time.t' to 'int', possible lose of data
Post your code
closed account (LyUoGNh0)
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
#include "windows.h"
using namespace std;

srand(time(0));
int game_Is_Running = 1;

int main ()
{

	// Intro
	cout << "Welcome to The Cave of Two Lovers." << endl;
	cout << "By OR Software." << endl;
	cout << "http://www.ORSoftware.netne.net/" << endl;
	system("pause");
	system("cls");

	int user_Input, right_Way, score;
	string character_Select,character_To_Go_With;

	// Game
	loop:
	cout << "You come across a cave with team avatar...." << endl;
	system("pause");
	cout << "Who do you want to be? (Aang, Sokka, or Katara)" << endl;
	cin >> character_Select;

	// Check what character they want to be
	if (character_Select == "Aang") {
		// Play as aang
		cout << "You have chosen to be Aang." << endl;
		system("pause");
		system("cls");
		cout << "You enter the cave..." << endl;
		system("pause");
		cout << "You walk in with Sokka, and Katara." << endl;
		system("pause");
		system("cls");
		cout << "The wall is starting to collapse. You have a choice to go with Katara or Sokka." << endl;
		system("pause");
		cout << "Who do you go with? HURRY YOU HAVE TO PICK SOON!" << endl;
		cin >> character_To_Go_With;
		if (character_To_Go_With == "Katara") {
			// Go with Katara
			int love; // How much love you and Katara have.
			system("cls");
			cout << "You and Katara are now alone...." << endl;
			system("pause");
			cout << "You can't stop thinking about your love for her," << endl;
			cout << "So that will delay you're thinking and reaction time." << endl;
			system("pause");
			system("cls");
			cout << "It's really dark. Katara asked you to hold her hand." << endl;
			cout << "But you also have torches." << endl;
			system("pause");
			cout << "What do you want to do?" << endl;
			int hold_Katara_Hand;
			cout << "(1 = Hold her hand ; 2 = Light a torch)" << endl;
			cin >> hold_Katara_Hand;
			system("cls");

			if (hold_Katara_Hand == 1) {
				// Continue through the cave holding her hand. (LOVE INCREASES BY 1)
				love = love + 1;
				cout << "You and Katara now have a love of " << love << endl;
				system("pause");
				system("cls");

				// The Which way to go loop
				while (game_Is_Running == 1) {
					// generate the right way
					right_Way = rand() % 2 + 1;

					cout << "You come across a two-way intersection. You can go left (1) or right (2)" << endl;
					cout << "Which way do you want to go?" << endl;	
					cin >> user_Input;
						
					if (user_Input == right_Way) {
						cout << "Congrates! You picked the right way!" << endl;
						score = score + 1;
						cout << "Your score is now " << score << endl;
						system("pause");
						system("cls");
					}else {
						cout << "You picked the wrong way..." << endl;
						score = score - 1;
						cout << "Your score is now " << score << endl;
						system("pause");
						system("cls");

					}		
				}
			}
Last edited on
Line 8: You can't call a function in the global scope. Put it inside main().
closed account (LyUoGNh0)
Thanks!
Topic archived. No new replies allowed.