cho-han bakuchi in basic C

Asked

Viewed 114 times

1

I am having a doubt in the program I made to represent the game cho-han bakuchi, this game has as a rule to roll two dice of 6 sides in a cup before to show the dice if it makes the bet in which the player speaks cho(pairs) or han(odd) and then the data are displayed and summed up their values.

in my program the game works except for the dice bearing, I am using the function time and srand this way:

srand(time(NULL));

The problem is that after the program is compiled and run the time(clock time on my pc) is saved then whenever the data scroll they will scroll the same number as the srand is saved in the schedule X for example 14:32:43 let’s say that the dice 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 would always like each given random Foce 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;
}

2 answers

2


srand(time(NULL));

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

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

But maybe milliseconds is still too slow (if you run the program in a loop for example). An alternative is to GetSystemTimeAsFileTime that advances 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 it using gettimeofday, thus:

#include <sys/time.h>
struct timeval time; 
gettimeofday(&time, NULL);
srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
  • ^^ thanks I’ll take a look =)) and that’s what I wanted to know, now if I need to put a number of random players from 1 to 5 players?

2

Your code is almost correct, just put this part inside the while:

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

Because you set the random numbers before the loop and didn’t change them again.

And note that the srand(time(NULL) should stay out of the loop to ensure that the numbers will be random.

  • Ita hahahaha truth as I did not flageei that thank you, anyway I would have noticed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.