Rolling dice in C++

Question:

I need a program that does the following:

  • Roll two dice (between 1 and 6).

  • If the value is the same, throw them again.

  • Print the total number of squares the player advances

Below I have the code; however, I can't find an if or similar function that can cast them again if both values ​​are equal.

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

int input (int cant)
{
  if (cant == 1)
    {
      std::cout << "Presione \"ENTER\" para lanzar el dado 1";
    }
  else
    {
      std::cout << "Presione \"ENTER\" para lanzar el dado 2";
    }
  std::cin.ignore ();
}


int tirardado ()
{
  int ran;
  srand (time (0));
  ran = rand () % 6 + 1;
  std::cout << "Obtuvo " << ran << std::endl;
  return ran;
}


int dado (int pdado, int sdado)
{
  std::cout << "Ha avanzado " << pdado + sdado << " casillas" << std::endl;
  return pdado + sdado;
}



int main ()
{
  int total, primerdado, segundodado;

  input (1);
  primerdado = tirardado ();
  input (2);
  segundodado = tirardado ();
  total = dado(primerdado, segundodado);
  return 0;
}

Answer:

Don't use rand .

You are programming in C++, however you follow C paradigms and use C++ utilities; do not use rand as it is not part of the C++ specification and therefore may not be portable and may give questionable results and performance. That is why deprecation is being studied .

Starting from the C++11 standard, the C++ language offers a complete pseudo-random number generation library that allows you to choose theprobability distribution (uniform, Bernoulli, Poisson, normal, discrete, constant, linear…), the underlying type of the generated value and even the algorithm to use (minstd, mt19937, ranlux, knuth…).

You're faking the 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 chance of being chosen (1 between RAND_MAX ).

Doing modulo ( % ) on the result of std::rand breaks homogeneity if the divisor is not a multiple of RAND_MAX . Assuming a RAND_MAX of 32767 with a modulus over 6 , we obtain that the numbers from 1 to 5 have a lower probability of appearing than 0 (0.003% lower).

Proposal.

Given the above, you could create a given object that included a homogeneous distribution of values ​​between 1 and 6:

template <int MIN, int MAX>
struct Dado
{
    int lanzar()
    {
        /* Generamos un número pseudo-aleatorio con el algoritmo
        mt19937 distribuido uniformemente entre MIN y MAX */
        return distribucion(generador);
    }

private:
    // Tenemos control sobre el algoritmo y distribución a usar.
    std::random_device device;
    std::mt19937 generador{device()};
    std::uniform_int_distribution<> distribucion{MIN, MAX};
};

With that Dado object you can create a function that follows your premises:

  • Roll two 6-sided dice.
  • If the value is the same, throw them again.
// Alias de dado de 6 caras.
using D6 = Dado<1, 6>;
// Nuestros dados.
D6 dado1, dado2;

int tirada()
{
    int tirada1, tirada2, avances{};

    do
    {
        avances += (tirada1 = dado1.lanzar()) + (tirada2 = dado2.lanzar());
        std::cout << "Primer dado: " << tirada1
            << "\nSegundo dado: " << tirada2
            << "\n\tAvances: " << avances << '\n';
    } while (tirada1 == tirada2);

    return avances;
}

Puedes ver el código funcionando en Wandbox Go to Sanhe (he՞ ਊ՞) Haha .

Scroll to Top