Thread: random array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    random array

    why is it that when i try to print out my cards my shuffle function is not changing the arrangement of my DECKSIZE array? more specifically the array cards[1] is the same for each print out....
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    
    typedef struct {
            int value;
            char* suit;
            char* name;
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for (i=0;i<6000;i++){
    int j = i+ rand()%(6000-i);
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
    temp=cards[this];
    cards[this]=cards[that];
    cards[that]=temp;
    }
    }
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                            cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                    cards[i].suit=suitName[suitCount];
                    cards[i].name=faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    {
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
            }
    int P_hand, D_hand;
    P_hand=rand() %52+1;
    D_hand=rand() %52+1;
    shuffleDeck();
    printf("%s of %s,----%s of %s\n ",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit );
    shuffleDeck();
    printf("%s of %s,----%s of %s\n",cards[1].name,cards[3].suit,cards[4].name,cards[2].suit  );
    shuffleDeck();
            return 0;
    }
    Last edited by gloworm; 04-12-2010 at 04:18 PM. Reason: my updated code

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    void shuffleDeck(){
    srand(time(NULL));
    You should only call srand() once in your program, not each time you want to shuffle.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    NeonBlack's got it covered - call srand() once, call it early in the program (preferably in main() ), and then NEVER CALL IT AGAIN.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    k

    your right got it but now i need to make sure that my cards are being asigned the right value and add those together, store that value, and then assign that value to my players hand: P_hand and my dealers hand: D_hand after that i am golden and can get this done! and i did update my code to what i have so far.
    Last edited by gloworm; 04-12-2010 at 04:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  3. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM