Thread: Random card generator

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Random card generator

    I’m starting to make a simple bridge style card game. And I have been able to get my program to generate random numbers then assign them to a card, but I am having a bit of trouble trying to work out how to make it not spit out the same card more than once. Anyone have any ideas?

    Below is the code i have come up with so far.

    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    // Random number generator, Will return a integer from 0 - 14
    int randomNumber(int d)
    {
        srand((unsigned)time(0));
        d = (rand()%14);
    }
    
    
    int main()
    {
        int i;
        while (i <= 52)
        {
        int card = randomNumber(card);
        //cout << card << " ";
        if (card == 14) cout << "A ";
        else if (card == 13) cout << "K ";
        else if (card == 12) cout << "Q ";
        else if (card == 11) cout << "J ";
        else cout << card << " ";
        Sleep(1000);
        i++;
        }
        system("pause");
    }
    oh and also, with the random number generator part, i am using the system clock to help make the numbers more random, but i can only get one number per second, is there another way to get random numbers? or is what i have the only way?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    All the card games I have seen have build a deck of cards and then shuffled them to get a random order. Also, you only have to call srand once in your program. You don't have to call it each time you call rand. To shuffle the cards just pick two random cards at a time and switch them. Do that alot and your deck will be pretty mixed up.
    Don't quote me on that... ...seriously

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Brad0407 View Post
    All the card games I have seen have build a deck of cards and then shuffled them to get a random order. Also, you only have to call srand once in your program. You don't have to call it each time you call rand. To shuffle the cards just pick two random cards at a time and switch them. Do that alot and your deck will be pretty mixed up.
    But if its just picking up random cards, how do you stop it picking the same card twice.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your randomNumber function either needs to take the parameter by reference (with '&') or return 'd'. it doesnt do what you want it to do, as is.

    i will try and write some pseudocode for this
    Code:
    declare array of 52 integers named 'deck'
    
    declare three integers, i (for counting) and rand (holds random number), and temp (for temporarily holding a 'card').
    
    for i is 0 to 51
       add next card (do each one sequentially.. as in having a 'sorted' deck) to index 'i' in 'deck' (deck[i])
    end loop
    
    for i is 0 to 51
       save card at index 'i' from array 'deck' (deck[i]) into 'temp'
       get random number from 'i' to 51 and store it in 'rand'
       move card from index 'rand' (deck[rand]) into index 'i' (deck[i])
       store card 'temp' into index 'rand' from 'deck' (deck[rand])
    end loop
    sorry if this makes no sense.. but im not making much right now. off to bed for exam tomorrow. hope it helps

    edit: get some paper and try and draw it out. either draw the method i have proposed, or better think of how you would do it. working things out on paper is incredibly helpful.
    Last edited by nadroj; 04-19-2007 at 11:51 PM.

  5. #5
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Thanks for the help mate, i think ill print out what you have there, and try to sketch it out...

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your welcome

    however, be aware! i think i see a logic error in the second loop (regarding the counter). i could fix it but then id have to think! which is what i dont want to do right now. so hopefully you can figure it out!
    i dont have complete faith in it, but its a starting point right?
    Last edited by nadroj; 04-19-2007 at 11:43 PM.

  7. #7
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Yeah, its something to get me going... i just had a mental block, and needed someone to snap me out of it.. hahaha

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think the latest edit (again) might work.. it just doesnt guarantee the last card (deck[51]) will have been 'shuffled'. i guess after the for loop you could just manually do a swap between deck[51] and deck[random] (using the same kind of logic as in the loop.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unfortunately, if I am reading it correctly nadroj's algorithm contains a common flaw that will not provide a truly random shuffle.

    >> But if its just picking up random cards, how do you stop it picking the same card twice.
    If you shuffle the deck, then you just pick cards from the deck one at a time and you will never get the same card twice. The key is to shuffle the deck properly to get an even distribution of possible shuffles.

    The link above describes the algorithm in one way (using a second empty deck and randomly selecting cards from the first deck to place in the second).

    Another way to look at the algorithm is basically the same, but only uses one deck. Randomly select one card from the deck and place it in the last spot in the array by swapping it with whichever card is there. Then randomly select another card in the array not counting the one at the end and swap it with the next to last card. Continue this way until there is only one card left that you swap with itself and you will have a properly shuffled deck.

    Of course, once you understand the proper algorithm for shuffling, I'd suggest not using it, but instead calling the C++ standard library function random_shuffle, which does this work for you (this code assumes you have a vector or array of 52 cards called deck):
    Code:
    std::random_shuffle(deck.begin(), deck.end()); // for a vector
    
    // or
    
    std::random_shuffle(deck, deck + 52); // for an array

  10. #10
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Quote Originally Posted by Daved View Post
    Unfortunately, if I am reading it correctly nadroj's algorithm contains a common flaw that will not provide a truly random shuffle.

    >> But if its just picking up random cards, how do you stop it picking the same card twice.
    If you shuffle the deck, then you just pick cards from the deck one at a time and you will never get the same card twice. The key is to shuffle the deck properly to get an even distribution of possible shuffles.

    The link above describes the algorithm in one way (using a second empty deck and randomly selecting cards from the first deck to place in the second).

    Another way to look at the algorithm is basically the same, but only uses one deck. Randomly select one card from the deck and place it in the last spot in the array by swapping it with whichever card is there. Then randomly select another card in the array not counting the one at the end and swap it with the next to last card. Continue this way until there is only one card left that you swap with itself and you will have a properly shuffled deck.

    Of course, once you understand the proper algorithm for shuffling, I'd suggest not using it, but instead calling the C++ standard library function random_shuffle, which does this work for you (this code assumes you have a vector or array of 52 cards called deck):
    Code:
    std::random_shuffle(deck.begin(), deck.end()); // for a vector
    
    // or
    
    std::random_shuffle(deck, deck + 52); // for an array
    I can't get that "random_shuffle" to work.

    Code:
        const string  deck[13]  = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
        std::random_shuffle(deck, deck + 13);
    when i try to compile it, it opens "stl_algo.h" and gives me this error
    1748 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h instantiated from `void std::random_shuffle(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = const std::string*]'

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as Daved mentioned, your deck needs to be a vector

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, the deck can be a vector or array (I usually prefer vectors, but for fixed sizes like this it doesn't matter so much). The problem is that the strings are const, and so they cannot be swapped by the shuffle. Just remove the const.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    21

    Using Pointers

    hey how would u do this using pointers without the random function any idea???

    have been trying this

    UNSUCCESSFUl...

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why do you think it would be easier with pointers? (And the random_shuffle algorithm already takes pointers...)

    Did you try removing the const keyword (you can't shuffle a constant array, because shuffling needs to modify it)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  2. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 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. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM