Need help, using random seed to generate card deck, odd output

This is a discussion on Need help, using random seed to generate card deck, odd output within the C Programming forums, part of the General Programming Boards category; Program background: I'm trying to simulate a 6-deck blackjack shoe, but only running through the first 3 decks; casinos shuffle ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Need help, using random seed to generate card deck, odd output

    Program background:

    I'm trying to simulate a 6-deck blackjack shoe, but only running through the first 3 decks; casinos shuffle at any point after the halfway, so I'm just assuming they shuffle immediately at the halfway.

    I'm trying to use the array deck to store each card (and I'm ignoring suit, so each card is merely 1-13). Array cardcheck's purpose is to hold how many of each card I've already used; in a 6-deck shoe, there are only 24 of each card, and randomly filling in each card slot, I have to check for each card that there are not already 24 of that card. If I were filling all 312 card slots, this would be inefficient as the deck approached full; however, because half the deck will remain unfilled, it will not have too many conflicts before picking an acceptable value.



    As it stands, the program is only supposed to generate a random deck and then output each card value on a separate line. The problem so far is I have not gotten the values expected; quite the contrary, running the program I get an alarming number of repetitive cards, and the values run far beyond the thousands in either direction.

    I'm sorry for not commenting my program well, I'm very new to coding.


    Without further ado, here's the program so far:

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    const int LOW = 1;
    const int HIGH = 13;
    
    int main()
    
    {
    
    int deck[156];
    
    int cardcheck[13];
    
    time_t seconds;
        time(&seconds);
        srand((unsigned int) seconds);
    
    for(int x=0;x<13;x++)
    {
    cardcheck[x]=0;
    }
    
    for(int x=0;x<156;x++)  // creates a 6 deck shoe, for first half of shoe
        {
        
        int newcard;
        
        newcard = rand() % (HIGH - LOW + 1) + LOW;  // creates random value in newcard
        
        if(cardcheck[newcard] < 24)
        {
         ++cardcheck[newcard];
         deck[x] = newcard;
        }
        else
        {
          x = x-1;    
        }
        
        for(int y=0;y<156;y++)
        {
        printf("%d \n", deck[y]);
        
        }
        
        }
    
    
    
    
    }

    My question boils down to, what is wrong with this? Further, are there any better ways to randomly fill in a set of values, where each value can only be used a certain number of times? Any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    For one thing, you're printing the entire deck 156 times, including cards that haven't been picked at all (if any). Take that print loop out of the card-picking loop.

    You're generating card numbers 1-13 and comparing them to card counts in the array cardcheck[13] elements [1] to [13], and incrementing elements [1] to [13], but that array consists of elements [0] to [12], so every time you add 1 to cardcheck[13] you're altering some other variable's memory space.
    Also, you're getting an unlimited number of 1's, and no 13's.

    But more importantly, you should rethink your entire approach to this. In reality, when a card is dealt, it is removed from the deck & therefore can't be dealt again. After an Ace, for example, has been removed, the probability of picking another Ace is reduced because the proportion of Aces in the part of the deck that remains has changed. Your algorithm, on the other hand, leaves the likelihood of picking any particular card always equal to the probability of any other card, 1/13.
    Last edited by R.Stiltskin; 11-06-2003 at 10:24 PM.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    You're right, I set the print command in the wrong spot. The end now looks like this:

    Code:
        else
        {
          x = x-1;    
        }
        
        }
        
        for(int y=0;y<156;y++)
        {
        printf("%d \n", deck[y]);
        }
    
    system("pause");
    
    }


    Thanks a ton for the help, tomorrow I'll fix that no-13-problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I generate a random number? (easily?)
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 05:04 AM
  2. How to generate random numbers the same everytime
    By lehe in forum C++ Programming
    Replies: 11
    Last Post: 04-09-2009, 06:00 PM
  3. Blackjack
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-20-2007, 08:07 PM
  4. How can I access a struct (from a header file)?
    By loxslay in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2006, 01:25 PM
  5. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21