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 ran;
srand (time (0));
ran = rand () % 6 + 1;
std::cout << "Obtuvo " << ran << std::endl;
return ran;
}

{
}

int main ()
{

input (1);
input (2);
return 0;
}
``````

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>
{
int lanzar()
{
/* Generamos un número pseudo-aleatorio con el algoritmo
mt19937 distribuido uniformemente entre MIN y MAX */
}

private:
// Tenemos control sobre el algoritmo y distribución a usar.
std::random_device 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.

{

do
{