random deck-card selection

I'm writing a card game and I'm having trouble getting the first part of it right, which is picking out a random card from the deck and displaying it to screen. So it's supposed to display a different card every time i run the function in the program. The problem is, I keep getting the same output every time "Nine of Hearts".

This is what I have so far,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

//declare two string arrays
//one holds the "suit" of the cards, the other hold the "value"
string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string faceValue[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ace", "King", "Queen", "Jack"};

//a function that randomly determines the suit and value of a card
string getCard()
{
    string card;   //a string that represents a card
 
//declare two integer variables that create random values

    int cardValue = rand() % 12;  //creates values between 0 and 11, for randomly determining the face value of a card

    int cardSuite = rand() % 4;  //creates values between 0 and 3, for determining the suit of a card
    
    card += faceValue[cardValue]; //Add the face value to the string "card"
    
    card += " of "; //a divider in-between the card value and suit 
    
    card += suit[cardSuite]; //Add the suit of the card to the string
    
    return card;  //output the string
}

int main()
{
   //output the randomly picked card to screen
    cout << getCard() << endl;
    return 0;
}



this is the output I keep getting every time I run the program:

Nine of Hearts



this "random selection" function I'm trying to do is meant to work as a means of shuffling the cards in the beginning of every round in the game and displaying only the top card from the deck.

any help is appreciated, Thanks!
Hi Sofia29,

You probably need to seed the random generator, as per this example :

http://www.cplusplus.com/reference/cstdlib/rand/


Hope all goes well :+)
Topic archived. No new replies allowed.