#why does my random generator generate the same

1 messages · Page 1 of 1 (latest)

idle fern
#

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

string random_number_generator () {
srand(time(NULL));
int random_number = rand() % 3;
string roll;
if (random_number == 0) {
roll = "rock";
}
else if (random_number == 1) {
roll = "paper";
}
else if (random_number == 2) {
roll = "scissors";
}
return roll;
}

int main (){
string player1, player2, player1roll, player2roll;
cout << "what is the name of player 1?";
cin >> player1;
cout << "what is the name of player 2?";
cin >> player2;

player1roll = random_number_generator();
cout << player1 << " got " << player1roll << endl;

player2roll = random_number_generator();
cout << player2 << " got " << player2roll << endl;

if (player1roll == player2roll) {
    cout << "The game is a tie." << endl;
} else if ((player1roll == "rock" && player2roll == "scissors") ||
           (player1roll == "paper" && player2roll == "rock") ||
           (player1roll == "scissors" && player2roll == "paper")) {
    cout << player1 << " is the winner" ;
} else {
    cout << player2 << " is the winner" ;
}

}

tiny crag
#

std::srand() returns the same numbers if you give it the same seed
std::time() returns the current time in seconds

#

Almost every time you run this program both calls will be in the same second and thus have the same result

#

You should use <random> instead of std::srand to avoid stuff like this

#

!cppref random

sour patrolBOT
tiny crag
#

!cppref <random>

sour patrolBOT
solemn badger
#

did you seed it?

tiny crag
#

srand(time(NULL)) twice

sour patrolBOT
#
#include <iostream>
#include <string>
using namespace std;

string random_number_generator() {
  srand(time(NULL));
  int random_number = rand() % 3;
  string roll;
  if (random_number == 0) {
    roll = "rock";
  } else if (random_number == 1) {
    roll = "paper";
  } else if (random_number == 2) {
    roll = "scissors";
  }
  return roll;
}

int main() {
  string player1, player2, player1roll, player2roll;
  cout << "what is the name of player 1?";
  cin >> player1;
  cout << "what is the name of player 2?";
  cin >> player2;

  player1roll = random_number_generator();
  cout << player1 << " got " << player1roll << endl;

  player2roll = random_number_generator();
  cout << player2 << " got " << player2roll << endl;

  if (player1roll == player2roll) {
    cout << "The game is a tie." << endl;
  } else if ((player1roll == "rock" && player2roll == "scissors") ||
             (player1roll == "paper" && player2roll == "rock") ||
             (player1roll == "scissors" && player2roll == "paper")) {
    cout << player1 << " is the winner";
  } else {
    cout << player2 << " is the winner";
  }
}
Clueless
obsidian cedar
#

yeah so normally if you do use rand() do srand() only once in your program in main

gleaming lotus
sour patrolBOT
#
Generating Random Numbers in C++

The <random> header in C++ provides (pseudo-)random number generation (PRNG):

Example: Printing Ten Random Dice Rolls
#include <random>
#include <iostream>
int main() {
  std::random_device dev; // for seeding
  std::default_random_engine gen{dev()};
  std::uniform_int_distribution<int> dis{1, 6};
  for (int i = 0; i < 10; ++i)
    std::cout << dis(gen) << ' ';
} ```
Possible Output (will be different each time)
1 1 6 5 2 2 5 5 6 2 ```
Common Generators