+ 1

how to remake random numbers in C++

I am trying to make a game of Rock Paper Scissors that after complied will be replayable but the random numbers I use for the starting game to pick the A.I.'s choice are reused for every following game. Can someone please explain why and how to fix it. my code: #include <iostream> #include <cstdlib> //used for rand() #include <ctime> //used for srand(time()) using namespace std; /* How to play 1.) hit run 2.) type rock, paper, or scissors 3.) hit sumbit 4.) read the output for the result WARNING!!! do NOT capitalize rock, paper, or scissors */ int main() { srand(time(0)); //uses systems time to make random number string player; char playing ='y'; int comp = 1 + (rand() % 3); //picks a randome number between 0 and 2 based off srand and adds 1 while (playing == 'y') { cout << "Rock...Paper...Scissors...1...2...3...Shoot" << endl; cin >> player; //players choice if (player == "rock") { if (comp == 1) { cout << "Rock! It's a tie" <<endl; } else if (comp == 2) { cout << "paper! I win!" <<endl; } else { cout << "Scissors! You win!" <<endl; } } else if (player == "paper") { if (comp == 1) { cout << "Rock! You win!" <<endl; } else if (comp == 2) { cout << "paper! It's a tie!" <<endl; } else { cout << "Scissors! I win!" <<endl; } } else if (player == "scissors") { if (comp == 1) { cout << "Rock! I win" <<endl; } else if (comp == 2) { cout << "paper! You win!" <<endl; } else { cout << "Scissors! It's a tie!" <<endl; } } else { cout << "invalid choice";

19th Feb 2022, 11:21 AM
Billy Stanley
Billy Stanley - avatar
2 ответов
+ 2
Add the comp variable in the while loop your srand is okay but you don't change the value of comp
19th Feb 2022, 11:31 AM
Мартин 😑🎵
Мартин 😑🎵 - avatar
0
I tried moving srand into the if statement and that didn't help
19th Feb 2022, 11:22 AM
Billy Stanley
Billy Stanley - avatar