How to randomly select a variable using Rand ()

I am trying to create a text fighter in C ++, this is one of the first things I did. So far I have this:

//Text Based Fighter #include <iostream> #include <stdlib.h> //srand, rand #include <string> using namespace std; int main() { //Player int playerHealth = 100; int attack1; int attack2; int attack3; string attack; int npc1; int npc2; cout << "Do you want to attack " << rand()[npc1,npc2]; //varname = rand() % 10 + 1; return 0; } 

I want this to be a random choice between npc1 and npc2, thanks.

Any comments on how I write my code will also be appreciated, I just started a couple of days ago, thanks, if you need more information, please feel free to ask, thanks.

0
source share
4 answers

You can simply use an array of an arbitrary number of variables:

 int attack[n]; //For some int-constant n attack[rand() % n]; //choose a random attack-variable, use it 
+1
source

For just 2 options, you can take the remainder of 2 with a triple expression:

 int choice = rand() % 2 == 0 ? npc1 : npc2; 

If you have more than two options, or even if you do not, you can create an array with these indices and index it.

 int npc_choices[2]; int choice = npc_choices[rand() % 2]; 

If the number of options is not 2, you are likely to introduce a very small bias in the selection using the modulo % operator. If you do not work for anything with statistical significance or with a huge number of options, I would not worry about that.

+1
source

If you have only two options in C ++ 11, you can use std :: bernoulli_distribution , and here is an oversimplified example:

 #include <iostream> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); // give "true"1/2 of the time // give "false" 1/2 of the time std::bernoulli_distribution d(0.5); int npcs[2] = {100, 101}; int index = d(gen) ? 0 : 1; std::cout << "Do you want to attack " << npcs[index] ; } 

using an array is more flexible, as it easily expands to more than two options, and then you will need to use std :: uniform_int_distribution to choose between [0,N] .

In the long run, using rand () is not a good idea , although in many simple cases this may work fine. As Pete mentions how long you have understood the limitations of rand() , you can use it, and there is a good section in the C FAQ, How can I get random integers in a specific range? .

0
source

It's easy to make mistakes when generating pseudo-random numbers. For example, in some cases, using rand() % RANGE may result in an erroneous distribution of numbers. (See this link for an example of a problem.)

It doesnโ€™t matter, itโ€™s trivial what you do.

If you need high-quality pseudo-random numbers, there are ways to fix rand() (see link above), but modern C ++ also provides <random> and uniform_int_distribution .

Here is an example simulating the casting of a hexagonal matrix, adapted from examples in Boost and the C ++ Reference :

 #include <iostream> #include <random> std::random_device rd; std::mt19937 gen(rd()); int roll_die() { std::uniform_int_distribution<> dist(1, 6); return dist(gen); } int main() { std::cout << roll_die() << std::endl; } 

The part that says dist(1, 6) can be changed to dist(0, 1) to create an output in the range [0, 1] (inclusive) with a uniform distribution.

0
source

Source: https://habr.com/ru/post/971357/


All Articles