Paul
Paul

Reputation: 515

rand() returns the same value after the first time

First, here's my code:

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

#define NUM_SUITS 4
#define NUM_RANKS 13

bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
bool newcard = {false};
int num_cards, rank, suit, totrank;
const char rank_code[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K',};
const char suit_code[] = {'C','D','H','S'};

int main_hand ()
{
    suit = rand() % NUM_SUITS;
    rank = rand() % NUM_RANKS;
    if (!in_hand[suit][rank]) {
        in_hand[suit][rank] = true;
        num_cards--;
        if (suit == 0){
            printf("%c of Clubs \n", rank_code[rank]);
        }
        else if (suit == 1){
            printf("%c of Diamonds \n", rank_code[rank]);
        }
        else if (suit == 2){
            printf("%c of Hearts \n", rank_code[rank]);
        }
        else if (suit == 3){
            printf("%c of Spades \n", rank_code[rank]);
       }
    }
}

int print_hand (suit)
{

}

int totrank_check (totrank)
{
    if (totrank > 21) {
        printf ("You lose!");
    }
    else if (totrank == 21) {
        printf ("You win!");
    }
}

int main()
{
     bool stay = {false};
     srand((unsigned) time(NULL));

     totrank = 0;
     num_cards = 2;
     printf("Your hand: ");
     while (num_cards > 0) {
         main_hand();
         totrank = totrank + (rank + 1);
     }
     printf("Total rank: %d\n", totrank);
     totrank_check(totrank);
     printf("\n");
     while (totrank < 24 && stay == false) {
        printf("Another card? 1. Yes 0. No\n");
        scanf("%d", &newcard);
        if(!newcard) {
            main_hand();
        }
        totrank = totrank + (rank + 1);
        totrank_check(totrank);
        printf("Total rank: %d\n", totrank);
        printf("Stay? 1. Yes 0. No\n");
        scanf("%d", &stay);
     }
    return 0;
}

Basically it's a code that "simulates" and hand of blackjack. It starts, rand() chooses two numbers, the rank and the suit of the cards, that are set as true in a matrix so that they can't be chosen again in the same combination and then printed. There's a check for the total rank of the cards (so that if it exceeds 21 you automatically lose) and then you are asked if you want another card or you want to stay.

Here's the error: if you choose that you want another cards, this new card will be the same as the last one. Basically, you get a Ace of Spades, the and Two of Diamonds, then you want another card and you get another Two of Diamonds. And the another, and another. If you remove the rank check in the second while you can see that the rank grows based on the rank of the last card.

Before, the printfs were in that print_hand() function, and you could see that you always got the same card, now I moved them in the main_hand() function because I thought that it might be the problem (it wasn't) and because having a separate function for the print was redundant. But you can see that technically, the if(!in_hand[suit][rank]) works, because, since the card is the same, it doesn't enter the if and it doesn't get printed.

I don't know what's causing this problem. Any idea?

Please note I'm already using srand((unsigned) time(NULL)); to seed rand().

Upvotes: 3

Views: 250

Answers (1)

chux
chux

Reputation: 153457

scanf("%d", &newcard); and scanf("%d", &stay); are a problem as the variables are bool, but the format specifier is for int. In 2 places, change like:

// scanf("%d", &newcard);
int t;
scanf("%d", &t);
newcard = !!t;

3 functions return int but do not return anything. Change to void functions.

// int main_hand() {
void main_hand() {    

There appears to be other logic issues too.
1) Code sometimes exists without knowing if won/loss
2) @Jongware comment above correct:

Lastly: If the same random sequence is still coming up, strip code to a bare main srand time printf rand and see if that works. time(), if not working always returns -1.
Or simple add the following and verify srand() called with different values.

 int main(void) {
   unsigned now = (unsigned) time(NULL);
   printf("now = %u\n", now);
   srand(now);

Upvotes: 3

Related Questions