How do I manage to run my random program? I can not identify the error

0
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
    {
        char *v[3] = {"piedra", "Papel", "tijeras"};
        int ju, pc, jugados = 1, ganados = 0, perdidos = 0;

            randomize();
            do
            {
                    printf("Jugada %d, elija una opcion (1, 2, 3) G: %d P: %d\n", jugados, ganados, perdidos);
                    printf ("1. Piedra\n");
                    printf ("2. Papel\n");
                    printf("3. tijeras\n");
                    do 
                    scanf ("%d", &ju);
                    while ((ju == '\n') && (ju != '1'|| ju != '2' || ju != '3'));
                    while (getchar() != '\n');
                    ju --;
                    pc = rand () % 3;
                    printf("Ud. eligio %s, la PC eligio %s => ", v[ju], v[pc]);
                    if (ju == pc)
                    printf ("Hay empate\n");
                    else if ((ju == 0 && pc == 2) || (ju == 1 && pc == 0)|| (ju == 2 && pc == 1))
                    {
                        printf ("Ud. gana\n");
                        ganados++;
                    }
                    else 
                    {
                        printf("La PC gana\n");
                        perdidos++;
                    }
                    jugados++;
                    printf("----------------------\n");
            }
            while (ganados < 5 && perdidos < 5);
            if (ganados > perdidos)
            printf ("Ud ha ganado en %d intentos.", jugados);
            else 
            printf("Ud ha perdido en %d intentos.", jugados);
            getchar();
            return 0;
    }
    
asked by Redes del Hack 16.11.2017 в 05:56
source

2 answers

1

What is happening to you is that the program is always initializing the random number generator with the same seed, which makes the sequence of random numbers the same execution after execution.

If you can not afford the luxury compile with C ++ 11 (2011 standard), and only for tests , since it's not a very secure option at the final program level, you can initialize the seed like this:

srand(time(NULL));

With this you initialize the seed with the current time of your system ... so each execution will give you different results. This solution is not very safe due to the low precision it offers, which makes it easier for someone to predict the random values obtained.

If compilation with C ++ 11 is allowed, it would be more advisable to use the library <random>

// Inicialización
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<int> rand(0, 3);

// números aleatorios
for( int i=0; i<10; i++ )
{
  int pc = rand(mt);
  std::cout << pc << ' ';
}
    
answered by 16.11.2017 в 08:22
0

First of all, this code is more C code than C ++ code. But after solving your problem with randomize() is able to compile for either of them. randomize() is a Borland style function, although it may still be available in some compilers, it is not part of the standard. The way to give a new seed to the random number generator so that each time the program is run the results are different, it is by srand() which including the header file time.h allows call function time(NULL) , finally replacing non-standard randomize(); with srand(time(NULL));

Your code looks like this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char* argv[])
    {
        char *v[3] = {"piedra", "Papel", "tijeras"};
        int ju, pc, jugados = 1, ganados = 0, perdidos = 0;

             srand(time(NULL)); 
            do
            {
                    printf("Jugada %d, elija una opcion (1, 2, 3) G: %d P: %d\n", jugados, ganados, perdidos);
                    printf ("1. Piedra\n");
                    printf ("2. Papel\n");
                    printf("3. tijeras\n");
                    do 
                    scanf ("%d", &ju);
                    while ((ju == '\n') && (ju != '1'|| ju != '2' || ju != '3'));
                    while (getchar() != '\n');
                    ju --;
                    pc = rand () % 3;
                    printf("Ud. eligio %s, la PC eligio %s => ", v[ju], v[pc]);
                    if (ju == pc)
                    printf ("Hay empate\n");
                    else if ((ju == 0 && pc == 2) || (ju == 1 && pc == 0)|| (ju == 2 && pc == 1))
                    {
                        printf ("Ud. gana\n");
                        ganados++;
                    }
                    else 
                    {
                        printf("La PC gana\n");
                        perdidos++;
                    }
                    jugados++;
                    printf("----------------------\n");
            }
            while (ganados < 5 && perdidos < 5);
            if (ganados > perdidos)
            printf ("Ud ha ganado en %d intentos.", jugados);
            else 
            printf("Ud ha perdido en %d intentos.", jugados);
            getchar();
            return 0;
    }
    
answered by 16.11.2017 в 08:32