Anonymous Helper
Anonymous Helper

Reputation: 41

How to output text from a random number

I am extremely new to c++, and I was wondering how I might output text from a random number generator. I am creating a text game. You occasionally fight things and I wish for whether you win or lose be random. For instance, if the random number is 2 (the only choices it would have would be one or two) then it would say: " You lost!". Please keep answers simple as I am very new and explaining your solution would be perfect.

Thanks in advance.

Upvotes: 0

Views: 990

Answers (5)

danielschemmel
danielschemmel

Reputation: 11116

Since so many usages of rand have been proposed here, let's do it a bit more robust:

  1. We will seed with std::random_device do ease into how <random> works. (You could use time(0) here, it does not really matter.)
  2. Our actual PRNG (the thing that makes numbers) will be [std::mt19937_64](http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine], which is accepted as one of the better random number generators.
  3. We will not simply inspect one bit, but tell C++ that we want a number in the range [0,1].
  4. We will combine this into a single object that you just need to call.
  5. A simple comparision will let us decide whether the player won or lost.

So, starting with number 1:

#include <random>
#include <functional>
#include <iostream>

int main() {
    using namespace std;  // because I am lazy today
    random_device seeder; // call this to get a number

    // more to do here
}

Now, while seeder() gives a random number, it is usually expected that you will just use this to seed your own PRNG (unless you do crypto, in which case it becomes much more complicated). So, let's do it:

mt19937_64 prng(seeder());

Well, that was easy. Now, let's make a distribution:

uniform_int_distribution<int> distribution(0, 1);

Now, to get an int that is either 0 or 1, we could just toss the prng to the distribution, as in:

int one_or_zero = distribution(prng);

But, that is cumbersome. So instead of the previous steps, we just combine everything:

auto dist = bind(uniform_int_distribution<int>(0, 1), mt19937_64(seeder()));

You can read this as "Make me a function-like variable named dist which holds a uniform distribution (every value is as likely as any other) of the range [0, 1] that is powered by an Mersenne Twister 64 PRNG.

All we now need to do is:

int one_or_zero = dist();

Ok, we just need to wrap a little if around a call to dist - sounds easy:

if(dist() == 0) {
    cout << "You won!\n";
} else {
    cout << "Sorry, you lost.\n";
}

You can see the result in action here, but be aware that the result is cached, so you'll need to fork it and run it yourself to see it change.

P.S.: Please note that it results in exactly two lines with the semantics similar to (swap it around a bit and you get exactly the same semantics) srand/rand -- except that it avoids a whole bunch of problems associated with those functions.

Upvotes: 0

LogicStuff
LogicStuff

Reputation: 19607

If there are only two options, the fastest way is to be interested only in value of the least significant bit.

if(randomNumber & 1) // equals 1 if the LSB is set.
    cout << "You won!" << endl;
else
    cout << "You lost!" << endl;

Upvotes: 0

julio uniqum
julio uniqum

Reputation: 58

Since your random out has only two states, you can think about it as flipping a coin, so you can take a random function and perform a modular division by 2, like this example (just look for 'coin toss' and you will get tons of samples):

http://www.c-program-example.com/2012/05/c-program-to-toss-coin-using-random.html

int toss = rand() % 2;

you can use toss to manage your chooses.

Upvotes: 0

Roaid
Roaid

Reputation: 314

#include<iostream>
using namespace std;
int main()
{int ran_num=0;
 srand((unsigned)time(0));
 while(ran_num !=2)   //You can add options here.
{ran_num=rand() % 100;//You can change the max number.
cout<<ran_num<<" "<<endl;
}
cout<<"You lost!";}

Upvotes: 0

szulak
szulak

Reputation: 703

#include <cstdlib>
#include <iostream>
#include <ctime>

int main() 
{
    std::srand(std::time(0)); // use current time as seed for random generator
    int random_variable = std::rand();
    std::cout << "Random value on [0 " << RAND_MAX << "]: " 
              << random_variable << '\n';
}

Source: http://en.cppreference.com/w/cpp/numeric/random/rand

Than, you can just compare it with your constant variable and do any action, ex.:

if (random_variable > 2)
    doSomething();
else
    doSomethingElse();

Upvotes: 1

Related Questions