2

I am working on a Tic-Tac-Toe game using C++ and I was wondering if it is possible to randomize a number but it could only choose either 2 or 5.

I made random start between user and the computer in order to make a fair start with these blocks of codes:

 srand(time(NULL));
 cout << "The First Person to go is randomly chosen!" << endl;
 FirsttoGo = (rand()%2) + 1;
 cout << "The first move goes to Player: " << FirsttoGo << endl;
 turn = FirsttoGo;

The feature that I would like to add is whoever starts the game will be able to pick either X or O but if the computer starts first, the computer should be able to pick between X or O. I also created constant values for X and O

static const int O = 5;
static const int X = 2;

Is there anyway to do this?

3
  • Don't use rand() for this.
    – 101010
    May 18, 2014 at 18:39
  • Perhaps ((int [2]){ 5, 2 })[rand() % 2]? May 18, 2014 at 18:39
  • 1
    The choice of 2 and 5 seems fairly arbitrary. Why not something simpler, and easier to randomly generate, like 0 and 1? May 18, 2014 at 19:12

4 Answers 4

5

In C++11 there are very nice and safer additions for the generation of random number in a range (see code below):

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(1, 2);
    for (int n=0; n<10; ++n) std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

Furthermore, in this nice talk Stephan T. Lavavej illustrates the reasons why using rand() considered harmful.

Based on the above example you could adjust it for your problem like this:

int getRandomIDX(int const a = 5, int const b = 2) {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> dis(1, 2);
  return (dis(gen) == 1)? a : b;
}
3
4
(rand() > RAND_MAX/2) ? 2 : 5;
0

Since you only need to choose between two values, you could already use the time function

time_t timer; 
time(&timer); // it returns an integral value

if the time timer is even pick the first one, if not pick the second .. that's real random, not pseudorandom

... someone hates the beauty of simplicity I guess. Go make random devices and distributions to pick between 2 values then

15
  • it's not real random. it absolutely no entropy. start 20 instances of the application within the same second and they will all output the same values.
    – example
    May 18, 2014 at 18:58
  • @example that's random dude, if you could tell me what the result would be I could give you some right. Start the afternoon and something else or who knows what will happen. May 18, 2014 at 18:58
  • Tell me the time at which you start it and I can predict precisely what your value will be. true randomness can only be gained quantum mechanically where even with full knowledge of every observable quantity the result will still be unknown until it is measured. (one often has to settle for "entropy" gained by events that (hopefully) are not oberved by others - like hardware interrupts - but even that is not true randomness)
    – example
    May 18, 2014 at 19:06
  • @example Nikos' method will work regardless of the seed method, I think you are perhaps reading into this more than necessary for a program that plays Tic-tac-toe. May 18, 2014 at 19:07
  • Tell me the seed to your random device and I'll also predict the output of the generator. "True" randomness refers to the player's choice of playing the game at any given time May 18, 2014 at 19:08
0

Here is the Fastest and Simplest way to pick a random no. between 2 number-

srand(time(0));
int t[2]={1,3}; //let two no. be 1 & 2
int l= rand() % (2); //get 0 or 1
cout<<t[l]<<endl;

That's it. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.