#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    342
    Rep Power
    0

    C source code to shuffle a single deck of cards ?


    Hi. I'm writing a blackjack program.

    I have an array of 52 numbers like

    cards[52]={2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6 ......11,11,11,11 }

    I want a way to sort or shuffle these numbers randomly but it doesn't have to be a professional randomization. Anyone know of a simple way to sort 52 numbers in an array randomly ?

    I was thinking of creating from scratch a 52 number array using a for loop. From this, somehow sorting my cards array with it. For example, whereever the number 3 falls in my SORTARRAY, it would take the 3rd number from my CARDS array and put it in that spot . Would this be a simple program ? It's the only way I've thought of doing it so far but if someone has something more simple , I'm all ears.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    In front of my Computer
    Posts
    40
    Rep Power
    10
    Code:
    for(i=0;i<52;i++)
    {
       p1 = rand()%52;
       p2 = rand()%52;
       
       t = cards[p1];
       cards[p1]=cards[p2];
       cards[p2]=t;
     }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    342
    Rep Power
    0
    won't that do some cards twice and create an improper deck of cards ?
  6. #4
  7. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    It may use the same numbers more than once. It may even use the same PAIR of numbers more than once. There's no reason for the loop to equal precisely 52, that being the case. However, it's a better shuffle than one usually does by hand with a real deck of cards. You asked for something less than professional.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    342
    Rep Power
    0
    It's ok. You misunderstood my question. I got it working, though. My code is pretty ugly but if anyone is interested, I'll post it but you'll probably laugh and find a lot of faults.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    342
    Rep Power
    0
    Code:
    array[8][11];
    I have an 8 x 11 matrix that I will be using in the program. I believe this is the way to write it ?

    if I wanted to assign values to it, how would I do that ?

    Would it be

    Code:
    array[3][4]={(61,61,61,61) , (62,62,62,62), (63,63,63,63)};
    Thanks. I haven't gotten my code to compile yet or I'd test it , myself.
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    array[3][4]={{61,61,61,61} , {62,62,62,62}, {63,63,63,63}};

    Otter work. Keep in mind that a shuffled deck has 52! different combinations (8.0658175170943878571660636856404e+67), the rand() supplied by most C/C++ libs is not up to the task. Whatever you develop, please be sure to note that true randomness is very difficult to achieve with a computer (though there are add-ons that can give you that and some OSs are able to achieve a reasonable pool of random data by looking at threads and process IDs, context switches, etc.). Keep in mind also that seeding your random number generator is also fraught with pitfalls. I have commented a time or twain on this subject if you care to search through these archives.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    342
    Rep Power
    0
    Thanks for the suggestion. Right now I'm using :

    Code:
    srand ( time(NULL) );
    I'm sure this isn't very good. Randomness is pretty important to me but I'm not sure how far I want to go with this since this is just for my own little project.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    FORGET ALL THIS, read
    /*
    generate an unique random shuffled bridge hand
    this is my c-version of an awk program extracted from:

    The AWK Programming Language, Addison-Wesley Copyright 1988
    by: Alfred V. Aho, Brian W. Kernigan, Peter J. Weinberger

    side 198 (try to understand the logic) [ quite old and powerfull ]

    NO WAYS TO GET TWICE THE SAME RESULT, even if you enter:
    prog;prog;prog;prog\n (> 10^6 times)

    guggach@yahoo.com

    [sure i have a c-version]
    */
    Last edited by guggach; April 15th, 2005 at 08:21 AM.
    working on Solaris[5-9], preferred languages french and C.
  18. #10
  19. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    As long as it is just a personal project, I wouldn't invest any more time in optimzing your random number generation. If you modularize how the cards are shuffled, you can change that part at any time without any impact to the rest of your program.

    I have been thinking about writing a shuffling program that people could plug into their program (the randomness would be based on the SHA2 hash algorithms, if you are interested), if you are interested I might get motivated enough to do so.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  20. #11
  21. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    sort or shuffle these numbers randomly but it doesn't have to be a professional randomization.
    Randomness is pretty important to me
    It helps if you make up your mind what you want before you go shopping.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Probably the simplest algorithm to shuffle an array:

    Code:
    for (int k = 52-1; k >= 0; --k) {
        int r = rand()%(k+1);
        int t = array[k];
        array[k] = array[r];
        array[r] = t;
    }
    Makes one pass through the array, and the resulting shuffle is exactly as good as your random number generator. I.e., if your RNG is perfectly random, then the shuffle is perfectly random.

IMN logo majestic logo threadwatch logo seochat tools logo