cho-han bakuchi in Basic C

1

I'm having a question in the program I made to represent the game cho-han bakuchi, this game has as a rule to roll two 6-sided dice in a cup before showing the dice if the bet is made in which the player speaks cho pairs) or han (odd) and then the data are shown and their values added.

In my program the game works except for the data roll, I'm using the time and srand function like this:

srand(time(NULL));

The problem is that once the program is compiled and run the time (clock time on my pc) is saved then whenever the data roll they will roll the same number because the srand is except in time X for example 14:32:43 let's say the data roll 4 and 4 would always like to know how I do to change this because no matter how many times they roll it will always be 4 and 4 I would like every given random data to always have what I do?

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

int die1;
int die2;
int sum;

int main()
{
    int x, cred=100, y, apost;

    srand(time(NULL));

    die1=1+(rand()%6);
    die2=1+(rand()%6);
    sum=die1+die2;

    while(cred!=0)
    {

        printf("faça a aposta:(digite -1 para sair)\n");
        scanf("%d", &apost);

        cred=cred-apost;

        printf("\nescolha 1-cho(par) 2-han(impar):\n");
        scanf("%d", &x);

         printf("\njogador rolou %d+%d=%d\n", die1,die2,sum);

        switch(x)
        {
            case 1:
                if(sum%2==0)
                {
                    printf("ganhou!\n");
                    cred=cred+(apost*2);
                    printf("seus creditos sao:%d\n",cred);
                }
                else
                {
                    printf("perdeu!");
                    printf("seus creditos sao:%d\n",cred);
                }
                break;

            case 2:
                if(sum%2!=0)
                {
                    printf("ganhou!\n");
                    cred=cred+(apost*2);
                    printf("seus creditos sao:%d\n",cred);
                }
                else
                {
                    printf("perdeu!");
                    printf("seus creditos sao:%d\n",cred);
                }
                break;
        }
    }

    return 0;
}
    
asked by anonymous 11.07.2014 / 16:44

2 answers

2
srand(time(NULL));

The problem here is that the value of time is the same every second. So if you open the program twice in the same second you will get exactly the same output. A simple alternative is to use the Windows function GetTickCount , which advances in milliseconds:

#include <windows.h>
srand(GetTickCount());

But maybe milliseconds are still too slow (if you run the program in a loop for example). One alternative is the GetSystemTimeAsFileTime advancement in 0.1 microseconds. This should be fast enough for any practical purpose.

#include <windows.h>
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
srand(ft.dwLowDateTime);

On Linux you can do this using gettimeofday , like this:

#include <sys/time.h>
struct timeval time; 
gettimeofday(&time, NULL);
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
    
11.07.2014 / 17:08
2

Your code is almost correct, you should only put this part within the while:

 srand(time(NULL));
 while(cred!=0)
    {
    die1=1+(rand()%6);
    die2=1+(rand()%6);
    sum=die1+die2;
 //...

Because you defined the random numbers before the loop and did not change them again.

And note that srand(time(NULL) must be outside the loop to ensure random numbers.

    
11.07.2014 / 17:13