Cube dice roll

3

I need a program that does the following:

• Toss two dice (between 1 and 6).

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

• Print the total number of boxes the player is advancing

Next I have the code; however, I can not find a function if or similar that can launch 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;
}

asked by André Enrique Brañez Quiroz 17.04.2018 в 19:24
source

3

Do not use rand .

You are programming in C ++, however you follow paradigms of C and use utilities of that language; 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 6 we get that the numbers from 1 to 5 have a probability of occurrence lower than the 0 (a 0.003% lower).

Proposal.

Taking into account the above, you could create a given object that includes 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 this Dado object you can create a function that follows your premises:

• Launch two 6-sided dice.
• If the value is the same, throw them again.
// Alias de dado de 6 caras.

{

do
{
<< "\n\tAvances: " << avances << '\n';

return avances;
}

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

source
3

I can not find an if or similar function that can launch them again if both values are the same.

That's because you have not tried to store the code in a separate function:

{
input (1);
input (2);
}

int main()
{
}

Of course you could also do it with a loop:

int main()
{
int contador = 0; // Para garantizar que solo repetimos la tirada una vez
do
{
input (1);
input (2);
}

By the way, note that the printing function can be simplified a lot. Also it does not return any value, then its return type should be void :

void input (int cant)
{
std::cout "Presione \"ENTER\" para lanzar el dado " << cant;
std::cin.ignore ();
}

Also, keep in mind that the initialization of the random number generator only needs to be done once, it would be recommendable that this initialization be moved to main :

int main()
{
srand (time (0));
// ...
}

Also, from C ++ 11 (remember that it is a standard that dates from 2011) the random library is available , which puts at your disposal a mechanism to generate random numbers much more secure and powerful and versatile than rand() , which is a function inherited from C.