Return a random value from an array

1

I am trying to get a function to return a random string from an array of strings but it looks like I am not passing the parameters to the function correctly. The problem is in the main.cpp

This is my code:

Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

class Player{
public:
  Player(std::string name, int health, int damage);

      void setName(std::string name);
      std::string getName() const;

      void setHealth(int health);
      int getHealth() const;

      void setDamage(int damage);
      int getDamage() const;
  private:
   std::string name;
   int health;
   int damage;
 };  
  #endif /* JUGADOR_H */
Player.cpp
#include "Player.h"

Player::Player(std::string name, int health, int damage){
setName("default");
setHealth(100);
setDamage(30);
 }

void Player::setName(std::string name)
{
this->name = name;
}

void Player::setHealth(int health){

health = 100;
}

void Player::setDamage(int damage){

damage = 30;
}

std::string Player::getName() const{

return name;

}

int Player::getHealth() const{

return health;
}

int Player::getDamage() const{

return damage;

}
main.cpp
#include <cstdlib>
#include "Player.h"
#include "Monster.h"
#include <iostream>
#include <string>

int main(int argc, char** argv) {
std::string name;
std::cout <<"Hello! Write your name "<<std::endl;
getline(std::cin, name);
Player player(name, 100, 30);
std::cout <<"Welcome "<<player.getName();


std::string monsters[3] = {"Orc", "Troll", "Undead"};
srand(time(NULL));
int ranM = (rand() % 3); //random monster

Monster monster(std::string monsters[ranM], 50,15);
std::cout <<"Suddenly you encounter a"<<monster.getName();
}
    
asked by JACK1ETO 14.06.2018 в 13:26
source

2 answers

3

This line:

Monster monster(std::string monsters[ranM], 50,15);

It should look like this:

Monster monster(monsters[ranM], 50,15);

It is only necessary to indicate the type to declare variables or to force conversions and this case does not occupy either of the two assumptions.

    
answered by 14.06.2018 / 13:30
source
2

Do not use rand .

You are programming in C ++, so do not insist on using C utilities; do not use rand as it is not part of the C ++ specification and therefore may not be portable and can offer questionable results and performance. Therefore, is being studied to deprecate it .

Starting with the C ++ 11 standard, the C ++ language offers a complete library of generation of pseudorandom numbers that allows to choose the probability distribution (uniform, Bernoulli, Poisson, normal, discrete, constant, linear ...), the underlying type of the generated value and even the algorithm to be used (minstd, mt19937, ranlux, knuth ...).

You are faking distribution.

The numerical distribution of std::rand is homogeneous between 0 and RAND_MAX , this means that any number within that range has the same probability of being selected (1 among RAND_MAX ).

When doing module ( % ) on the result of std::rand you break the homogeneity if the divisor is not a multiple of RAND_MAX . Assuming a RAND_MAX of 32767 with a module on 3 we obtain that the number 0 has an occurrence probability greater than the 1 and the 2 (a 0.0031% higher).

Proposal.

Use the C ++ pseudo-random number utilities:

int main(int argc, char** argv) {
    std::string name;
    std::cout <<"Hello! Write your name "<<std::endl;
    getline(std::cin, name);
    Player player(name, 100, 30);
    std::cout <<"Welcome "<<player.getName();

    std::string monsters[3] = {"Orc", "Troll", "Undead"};

    // Tenemos control sobre el algoritmo y distribución a usar.
    random_device device;
    // Se usa el algoritmo Mersenne twister
    // https://es.wikipedia.org/wiki/Mersenne_twister
    mt19937 generador(device());
    // Escogemos una distribucion uniforme entre 0 y 3
    uniform_int_distribution<> distribucion(0, 3);

    /* Generamos un número pseudo-aleatorio con el algoritmo
    mt19937 distribuido uniformemente entre 0 y 3
                                         vvvvvvvvvvvvvvvvvvvvvvv */
    Monster monster(std::string monsters[distribucion(generador)], 50,15);
    std::cout <<"Suddenly you encounter a"<<monster.getName();

    return 0;
}

Other things to consider.

answered by 14.06.2018 в 14:33