2

I am trying to build a poker game using C++. The deck shuffling function is giving me some issues. Every time that I run the program that initializes the deck, shuffles it, and then prints the deck I get the same output:

Shuffling the cards and dealing...
Printing deck...
KD
6S
7D
QD
5C
JH
9S
6D
7H
JD
QH
3C
7S
3H
TC
5D
5S
3D
AD
7C
4H
6H
JC
TS
4D
JS
QC
AH
9C
2D
5H
8C
TD
4S
2S
KS
2C
8D
KC
2H
9H
6C
KH
3S
QS
8S
8H
4C
AS
AC
9D
TH

Using the classes Deck and Card I have the relevant functions defined as follows:

Deck::Deck(){
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 13; j++) {
            cards[i * 13 + j].suit = i;
            cards[i * 13 + j].rank = j;
        }
    }
    Card::suits[0] = "D";
    Card::suits[1] = "S";
    Card::suits[2] = "H";
    Card::suits[3] = "C";
    Card::ranks[0] = "2";
    Card::ranks[1] = "3";
    Card::ranks[2] = "4";
    Card::ranks[3] = "5";
    Card::ranks[4] = "6";
    Card::ranks[5] = "7";
    Card::ranks[6] = "8";
    Card::ranks[7] = "9";
    Card::ranks[8] = "T";
    Card::ranks[9] = "J";
    Card::ranks[10] = "Q";
    Card::ranks[11] = "K";
    Card::ranks[12] = "A";
}

void Deck::print(){
    cout << "Printing deck..." << std::endl;
    for (int i = 0; i < 52; i++) {
        cout << Card::ranks[cards[i].rank] << Card::suits[cards[i].suit] << endl;
    }
    cout << endl;
}

void Deck::shuffle(){
    top = 51;
    int x;
    Card tempCard;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 13; j++) {
            cards[i * 13 + j].suit = i;
            cards[i * 13 + j].rank = j;
        }
    }
    cout << "Shuffling the cards and dealing..." << endl;
    for (int i = 0; i < 52; i++) {
        x = rand() % 52;
        tempCard = cards[i];
        cards[i] = cards[x];
        cards[x] = tempCard;
    }
}

Is there something that I am doing wrong? Why do I always get the same result when it should be random? Thanks.

wilx
  • 17,697
  • 6
  • 59
  • 114
Todd Baker
  • 41
  • 1
  • 9

4 Answers4

5

Assuming you can use C++11 features, you can use this (taken from https://stackoverflow.com/a/19728404/341065):

#include <random>

std::random_device rd;     // only used once to initialise engine
std::mt19937 rng(rd);      // random-number engine used
std::uniform_int_distribution<int> uni(min,max); // guaranteed unbiased

auto random_integer = uni(rng);
Community
  • 1
  • 1
wilx
  • 17,697
  • 6
  • 59
  • 114
  • My answer is more of a minimal fix, but this answer is a much better approach. Note too, that `std::random_device` should, with just about every implementation, provide much better entropy than `srand(time(NULL))`. – Edward Jan 03 '15 at 22:28
3

Your code has several problems with regard to randomness:

Community
  • 1
  • 1
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
1

You need to seed the random number generator. Specifically call srand() once at the beginning of the program. One common way to do this is:

srand(time(NULL));
Edward
  • 6,964
  • 2
  • 29
  • 55
-1

the rand() method, if not initialized, return always the same "random" number. You have to initialize the starting point of the generation of the numbers with the function srand(). This function take a parameter which allows to generate different numbers. Put this line

srand(time(0));

before the rand function and you will have always different numbers. (time(0) is the system time. You can put here a number like 100, but you will have always same number. with time(0) the number will change every time).

Frank Cunningham
  • 178
  • 1
  • 12
  • I'm not sure if this why your answer was downvoted, but you might want to clarify that you should call `srand(time(0))` only *once* before calling `rand()`, not every time. – jamesdlin Jan 03 '15 at 21:41
  • It was probably downvoted because _"the rand() method, if not initialized, return always the same 'random' number"_ can be interpreted to mean that `rand()` has the same result every time it's called, if said calls are not preceded by one to `srand()`. It would be better to discuss how a _sequence of calls_ will result in the same _sequence of results_ in such a scenario. – Lightness Races in Orbit Jan 03 '15 at 21:52