Question

I'm trying to create a procedural that draws cards from a deck at random. The problem was that I needed to make the draw procedure actually random, as each draw is the same when using srand and rand when no other factors change. Thus I attached it to time_t seconds and was able to make the draw semi/ random. This is because I have to wait for a second to go by for the parameter of time_t seconds to change. The reason this is a problem is that my program is made to draw again if it draws the same card twice (which it's known to do). Thus if it does draw the same card twice it will be forced to draw around fifteen more times (or more or less) until the time_t second parameter changes. Is there a way to measure milliseconds or an otherwise smaller unit of time for time so I don't have this issue?

Here is the code, though the checking for match and organizing proceedures are not attached (those only occur after the initial draw anyway.)

int allow = 0;
    string cards[] = 
    {"02hearts", "03hearts", "04hearts", "05hearts", "06hearts", "07hearts", "08hearts","09hearts", "10hearts", "11hearts", "12hearts", "13hearts", "14hearts",
     "02clubs", "03clubs", "04clubs", "05clubs", "06clubs", "07clubs", "08clubs", "09clubs", "10clubs", "11clubs", "12clubs","13clubs", "14clubs","14clubs", 
    "02spades", "03spades", "04spades", "05spades", "06spades", "07spades", "08spades", "09spades", "10spades", "11spades", "12spades", "13spades", "14spades",
    "02diamonds", "03diamonds", "04diamonds", "05diamonds", "06diamonds", "07diamonds", "08diamonds", "09diamonds", "10diamonds", "11diamonds", "12diamonds", "13diamonds", "14diamonds"};
    string cardHand [5];
    string cardnumbers [5];
    int cardInts [5];
    string handSuites [5];
    char handSuitesChar [5];





    //check deck
    while(allow == 0)
    {

    //set clock
    time_t seconds;

    time(&seconds);


    srand((unsigned int) seconds);

    int type;

    //initiate counters
    int n = 0;
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int n4 = 0;

    //draw cards
    while(n < 5)
    {
        type = rand() % 52;
        cardHand[n] = cards[type];
        cout << cardHand[n] << ", ";
        n++;
    }


    cout << endl;

    //pull numbers from cards
    while(n1 < 5)
    {
        string character2;
        cardnumbers[n1] = cardHand[n1].at(0);
        character2 = cardHand[n1].at(1);
        cardnumbers[n1].append(character2);
        cout << cardnumbers[n1] << ", ";
        n1++;
    }
    cout << endl;
    cout << endl;


    //convert numbers to ints
    while(n2 < 5)
    {
        stringstream convert(cardnumbers[n2]);

        if( !(convert >> cardInts[n2]))
            cardInts[n2] = 0;

        cout << cardInts[n2] + 100 << ", ";

        n2++;
    }

    cout << endl;

    //pull out first letters for suites
    while (n3 < 5)
    {
        handSuites[n3] = cardHand[n3].at(2);

        cout << handSuites[n3]<< endl;
        n3++;
    }


    //convert letters to chars
    while (n4 < 5)
    {
        stringstream convert(handSuites[n4]);

        if( !(convert >> handSuitesChar[n4]))
            handSuitesChar[n4] = 0;

        cout << handSuitesChar[n4] + 100 << ", ";
        n4++;
    }
Was it helpful?

Solution

Don't call srand() inside the loop. Call it once at the start of your program, and then just use rand() whenever you need a random number. That'll give you a fairly long pseudorandom sequence.

OTHER TIPS

As Rob pointed out, the problem isn't with rand(), but with the way you are drawing the cards. There's no reason to every draw the same card twice. You can use Rob's suggestion, and shuffle the deck, or you can pick a random card from an unshuffled deck, and then remove it from the deck (swap with the end, then pop_back), so that it can't be drawn again.

And of course, never seed the generator more than once in a single process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top