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?

tuygun
  • 79
  • 3
  • 12

4 Answers4

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;
}
101010
  • 41,839
  • 11
  • 94
  • 168
4
(rand() > RAND_MAX/2) ? 2 : 5;
BonzaiThePenguin
  • 1,413
  • 13
  • 19
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

Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • 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 '14 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. – Nikos Athanasiou May 18 '14 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 '14 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. – IllusiveBrian May 18 '14 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 – Nikos Athanasiou May 18 '14 at 19:08
  • @NikosAthanasiou that is why it is called a _pseudo_ random number generator. obviously a mersenne twister is not truly random (though definitely enough for a tic-tac-toe game) – example May 18 '14 at 19:12
  • @example Can you read what I'm saying in the answer before downvoting or comment-harassing ? That's why I'm refering to those generators as pseudo-random. (it's the highlighted text) – Nikos Athanasiou May 18 '14 at 19:15
  • you said: "Tell me the seed to your random device and I'll also predict the output of the generator." -> "yes, it is a PRNG", "Tell me the time at which you start it and I can predict precisely what your value will be" -> the time is also pseudo random at best. – example May 18 '14 at 19:21
  • @example What you fail to get, is that I can deterministically tell what the seed is ... by looking at the code. Even if another pseudorandom number whas used, I could trace its origins. In the other case, there is no deterministic way to tell what time the player will play the game; knowing the time the game will be played can be done only retro-spectively (or by predicting the future). Once the game ships, a single seed would produce the same sequence of random numbers. The only way to make a fuzzy behavior out of it would be ... oh wait ... to use the time as a seed. – Nikos Athanasiou May 18 '14 at 19:28
  • @example Now unless you can prove that free will is deterministically controlled, be a good sport, learn to loose and upvote – Nikos Athanasiou May 18 '14 at 19:29
  • under other circumstances I would even agree, that a _high precision_ clock might even give a few bits of entropy - but if you write a tic-tac-toe game based on your code, all a player has to do to always have `X` start the game is look at the corner of his screen to see whether the sconds on his clock are even or odd. that is not random. – example May 18 '14 at 19:40
  • I don't think you're seriously suggesting that `time()` is a suitable replacement for a PRNG but the questioner is just learning C++ and deserves a better, more useful, answer. – Blastfurnace May 18 '14 at 19:45
  • @Blastfurnace Never suggested such a thing. My answering was analogous to the tone of the debate. I'd suggest reading into `engines` and `distributions` which are the tools for randomness in C++11. I try to write about having a `uniform_int_distribution` of 2 ints => each one of its members has a 50% propability of manifestation , how the responsibility could easily pass solely to the engine, what it means to have the engine's output converted to one of two numbers and how this could be emulated by a dualistic property of integrals; the comments section is too short man, I'm sorry. – Nikos Athanasiou May 18 '14 at 20:24
  • I'm well aware of the C++11 `` facilities and I see you've added some links after the fact. So now you've posted a combination of absurd suggestion and link-only answer. I'll just assume you're trying to be humorous and leave you to it. Cheers. – Blastfurnace May 18 '14 at 20:35
  • @Blastfurnace What links are you refering to? The ones in my answer? These were up more than an hour ago. Were posted after the first downvote. The "I'd suggest .." part or the "I try .. " are a response to you saying example is learning c++ ; better yet it's me trying to explain my rationale to him; I'm replying to you just because you were the one to point out I should be more thorough in my answers, I'm not trying to school you man. – Nikos Athanasiou May 18 '14 at 20:41
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!

Yash
  • 1,787
  • 1
  • 22
  • 23