Drawing Cards from a deck keep drawing same card

c2 and c3 are the same number but i want them to be different numbers. the reasons its the same random is because i want to keep track of whats being "drawn" and make sure the player cant draw the same card more than 4 times and draw more than 52 card.

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
  #include <iostream>
#include <ctime>
using namespace std;
int DeckDraw();
int main()
{
	int c2 = DeckDraw();
	int c3 = DeckDraw();
	cout << c2 << endl;
	cout << c3 << endl;
	system("pause");
	return 0;
}
int DeckDraw()
{
	int r1 = 0, r2 = 0, r3 = 0, r4 = 0, r5 = 0, r6 = 0, r7 = 0, r8 = 0, r9 = 0, r10 = 0, r11 = 0, r12 = 0, r13 = 0;
	int Draw;
	char Next = 'N';
	int DeckSpew;
	srand(time(0));
	while (Next == 'N')
	{
		DeckSpew = 1 + rand() % (13 - 1 + 1);
		if (DeckSpew == 1)
		{
			r1++;
		}
		if (DeckSpew == 2)
		{
			r2++;
		}
		if (DeckSpew == 3)
		{
			r3++;
		}
		if (DeckSpew == 4)
		{
			r4++;
		}
		if (DeckSpew == 5)
		{
			r5++;
		}
		if (DeckSpew == 6)
		{
			r6++;
		}
		if (DeckSpew == 7)
		{
			r7++;
		}
		if (DeckSpew == 8)
		{
			r8++;
		}
		if (DeckSpew == 9)
		{
			r9++;
		}
		if (DeckSpew == 10)
		{
			r10++;
		}
		if (DeckSpew == 11)
		{
			r11++;
		}
		if (DeckSpew == 12)
		{
			r12++;
		}
		if (DeckSpew == 13)
		{
			r13++;
		}
		if (r1 <= 4 && r2 <= 4 && r3 <= 4 && r3 <= 4 && r5 <= 4 && r6 <= 4 && r7 <= 4 && r8 <= 4 && r9 <= 4 && r10 <= 4 && r11 <= 4 && r1 <= 4 && r12 <= 4 && r13 <= 4)
		{
			Next = 'Y';
			Draw = DeckSpew;
		}
	}
	return Draw;
	Next = 'N';
}
> srand(time(0));
You should call this exactly ONCE at the start of main.

time(0) is going to be a constant in your short-lived program.

> int r1 = 0, r2 = 0, r3 = 0, r4 = 0, r5 = 0, r6 = 0, r7 = 0, r8 = 0, r9 = 0, r10 = 0, r11 = 0, r12 = 0, r13 = 0;
These are re-initialised to 0 every time you call the function.
If you want to remember where you got to, you should make them static.


Also, line 83 will NEVER happen, as it's after the return statement.


Why zillion integers r1..r13 when you could have an array:
1
2
int draws[13] {};
++draws[ rand() % 13 ];

Dialing cards? Look here:
http://www.cplusplus.com/forum/general/252307/#msg1110816
Lines 25 and 26. The trick is, the program "knows" which bit stands for what avers in which suit. An additional deck is just changing one constant.

Hint: Instead of riffle the cards I keep the deck in sorted order and pick cards at random. Not to pick that card a second time I mark it as already dialed out. Simple as can be.
Last edited on
You have 13 cards in order. You pick one in random. You put it back exactly where it was. You hope that you don't choose the same card multiple times. Stage magic does almost that in the "I know which card you saw" trick.


A dealer has a deck of cards.
The dealer shuffles the deck at start.
The dealer deals from the top of the deck and never puts any cards back.
When the deck is empty, it is empty.


Two very different procedures. Which one do you actually try to model?
To finish keskiverto's point:

To randomly choose things without repeat:
• create a list (array/vector/whatever) of things with no repeats
randomize the list
• take the first item, then the second, and so on

For a deck of cards, you need a list of 1..52
Suite == card number / 13
Rank == card.number % 13

Good luck!
Topic archived. No new replies allowed.