Why doesn't rand() work properly?!

closed account (oN3h0pDG)
Hello, I'm trying to write a card game program and i'm really banking on rand() to work, and it's giving me the same value each time which is not good. Is there anything I can change in my code to make it select different random numbers? Thanks!

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

// record containing 1 playing card
struct Card {

	char cardSuit; // Hearts, Diamonds, Clubs, or Spades
	int cardValue; // Card value from 1 to 13

};

// Random card generator that picks a random number between 1 and 52
int randomCard() {

	srand (time(NULL));
	return rand() % 52;

}


void displaySingleCard(Card deck[52]) {
int random;

random = randomCard();


if (deck[random].cardValue == 1) { // Ace

	cout << deck[random].cardValue << "A ";

} else if (deck[random].cardValue == 11) { // Jack

	cout << deck[random].cardValue << "J ";

} else if (deck[random].cardValue == 12) { // Queen

	cout << deck[random].cardValue << "Q ";

} else if (deck[random].cardValue == 13) { // King

	cout << deck[random].cardValue << "K ";

} else {

cout << deck[random].cardValue << deck[random].cardSuit << " ";

}
}

void displayCards(Card deck[52], int cardSize) {

	for (int x = 0; x < cardSize; x++) {

		displaySingleCard(deck);

	}

}

// Procedure that initilizes
void initilize(int cardSize) {


	Card deck[52] = {'H', 1, 'H', 2, 'H', 3, 'H', 4, 'H', 5, 'H', 6, 'H', 7, 'H', 8, 'H', 9, 'H', 10, 'H', 11, 'H', 12, 'H', 13, // Hearts
					'D', 1, 'D', 2, 'D', 3, 'D', 4, 'D', 5, 'D', 6, 'D', 7, 'D', 8, 'D', 9, 'D', 10, 'D', 11, 'D', 12, 'D', 13,	// Diamonds
					'C', 1, 'C', 2, 'C', 3, 'C', 4, 'C', 5, 'C', 6, 'C', 7, 'C', 8, 'C', 9, 'C', 10, 'C', 11, 'C', 12, 'C', 13,	// Clubs
					'S', 1, 'S', 2, 'S', 3, 'S', 4, 'S', 5, 'S', 6, 'S', 7, 'S', 8, 'S', 9, 'S', 10, 'S', 11, 'S', 12, 'S', 13,}; // Spades

	displayCards(deck, cardSize);

}


int main() {
	int cardSize = 5;

	initilize(cardSize); // Initilize deck



	return 0;
}
You are calling srand() every time you call rand().

Don't do that.

You should call srand() exactly once to initialize the RNG. Once you do that you shouldn't call it again.
line 18: srand() is in the wrong place. srand() seeds the random number generator with the seed value given. Because you're calling randomCard() inside a loop, srtand() is always seeding the RNG with the same time value, resulting in rand() always returning the first value in the sequence of pseudo-random numbers.

srand() should only ever be called ONCE at the beginning of main.

closed account (oN3h0pDG)
Thank you all! I wasn't aware of that, thank you so much!!!
rand() is really bad. Use the stuff from <random> header...
rand() is sufficient for this kind of game.
More on C-rand: http://www.cplusplus.com/faq/beginners/random-numbers/
Topic archived. No new replies allowed.