Simple Blackjack Game, Random Numbers

hey guys, I am writing a program for a simple game of Blackjack, but when it runs, every card comes out the same. Please, any help is appreciated.

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
//Blackjack Program
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

#define HIGH 11
#define LOW 1
int dealfirst;
int dealsecond;
int playfirst;
int playsecond;
int temp;
time_t seconds;

void deal (bool faceup) //deal function
{
	time_t seconds;
	time(&seconds);
	srand((unsigned int) seconds);
	temp = rand() % (HIGH - LOW + 1) + LOW;
	if (faceup == true)
		if (temp != 11)
			cout << temp << endl;
		else
			cout << "Ace" << endl;
	else 
		cout << "Unkown  card \n";
}

int main ()
{

	cout << "Blackjack Program \n";
	cout << "Dealer's cards:" << endl;
	deal (true); //Deals first card
	dealfirst = temp;
	time(&seconds);
	srand((unsigned int) seconds);
	deal (false); //deals second card
	time(&seconds);
	srand((unsigned int) seconds);
	dealsecond = temp;
	cout << endl << "Your cards" << endl;
	time(&seconds);
	srand((unsigned int) seconds);
	deal (true); //Deals your first card
	time(&seconds);
	srand((unsigned int) seconds); 
	playfirst = temp;
	deal (true); //deals your second card
	playsecond = temp;
	cout << "Hit (H) or Stand (S)?" << endl;






return (0);
}
It's because you are calling srand() with the *same* value everytime you want a random number. Just do: srand(time()); once, then your rands will be fine.
Ok, first of all, thank you for the help, but I don't exactly understand. It says that time func doesn't take 0 arguments. So I put srand (time (&seconds)) but that didn't make the numbers different either. Should it go right at the beginning of the main function or right before every time I call the deal function?
Try srand(time(NULL)); then.

And yes, put it right at the beginning of main, you don't need to call it more then once.
Thank you. My problem is solved. I ran the program 4 times, and came out with all different cards every time.
Topic archived. No new replies allowed.