### 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
 ``123456789101112131415161718192021222324`` ``````// 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;
 ``12`` ``````#include #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.
 ``123456`` ``````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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include "stdafx.h" #include #include #include #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"); } } }``````