1

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

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" a 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 tought 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? I tried moving srand() in the function just above rand() but nothing changed. It's strange, because it works fine the second time, but the third and on it doesn't. I feel like it's something obvious that I'm missing.

(I already asked this question before, but it was marked as duplicate by someone who didn't even bother to read it, so I didn't get any useful answers.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Paul
  • 473
  • 6
  • 19
  • Can you post minimal code that demonstrates the issue? – tmr232 Dec 18 '14 at 21:04
  • I tried your code and I get different random numbers. Tested with gcc 4.7.3. – R Sahu Dec 18 '14 at 21:09
  • clang-600.0.56 also gives different random numbers. – evading Dec 18 '14 at 21:13
  • 2
    `main_hand ()`, `print_hand (suit)`, `int totrank_check (totrank)` are supposed to return an `int`, but no return value provided. With a good compiler and warnings enabled, this readily comes up. Suggest enabling all warnings and fixing those issues before posting - it saves you time. Previous post re-opened(). – chux - Reinstate Monica Dec 18 '14 at 21:19
  • 1
    Posted answer in [original post](http://stackoverflow.com/questions/27411492/rand-returns-the-same-value-after-the-first-time) – chux - Reinstate Monica Dec 18 '14 at 21:30
  • Sorry to have contributed to the errant original closing of the first post. My rational for that, in review, was weak. – chux - Reinstate Monica Dec 18 '14 at 21:32
  • @R Sahu @evading How's that possible? I tried on gcc 4.7.3 and on the last version that comes with codeblocks (I don't remember which version of gcc it uses) and I get the same result after the first time. – Paul Dec 18 '14 at 21:34
  • @chux don't worry. :) Thanks for the reply! – Paul Dec 18 '14 at 21:35

0 Answers0