[HELP] random_shuffle with Deck

I am trying to made a card game...
I want to use random_shuffle instead of rand() but i dont know how to do it.
With rand() i did:

1
2
3
4
5
     number[x] = rand() % 13 + 1;
     type[x] = rand() % 4 + 3;

//number(1,2,3,4,5,6,7,8,9,10,J,Q,K)
//type(heart, diamond, club, spade) 


I wanna use the random_shuffle instead of rand() because i dont want the get the same card twice.

Thank you :D
Last edited on
I think you can use this:timeGetTime()+rand()%13system will join when you generation digital and number will depend on when you push the button.Millisecond is very quick and you can't control it.This is my original.
I use timeGetTime(), but sometimes I get the same card twice and I dont want it to happen. I need first to make all the cards in the deck, and then do random_shaffle but I dont know how :(
Last edited on
Here's a card shuffle program I knocked up in a couple of minutes that uses random_shuffle.

Note, I was too lazy to abstract that cards with a value of 1 are aces and 11, 12 and 13 are the face cards.

It's an easy example of how to use random_shuffle, anyhow. You could expand upon it by including a pointer to some random number generator.

As per usual, excuse any crazy Visual Studio indentation.

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct Card
{
	string suit;
	unsigned short value;
};

int main( int argc, char* argv[] )
{
	vector<Card> deck;
	vector<Card>::iterator it;
	string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
	Card temp;

	for ( int i = 0; i < 4; i++ )
	{
		temp.suit = suits[i];
		for ( int j = 1; j < 14; j++ )
		{
			temp.value = j;
			deck.push_back( temp );
		}
	}

	it = deck.begin();

	cout << "UNSHUFFLED: \n";
	while ( it != deck.end() )
	{
		temp = *it;
		cout << temp.value << " of " << temp.suit << endl;
		it++;
	}

	random_shuffle( deck.begin(), deck.end() );

	it = deck.begin();

	cout << "SHUFFLED: \n";
	while ( it != deck.end() )
	{
		temp = *it;
		cout << temp.value << " of " << temp.suit << endl;
		it++;
	}
}
Last edited on
I suppose there's no point re-inventing the wheel; if random_shuffle does the job, you may as well use it.

In a more primitive environment, I use this method to avoid duplicates.

First, select an item randomly from the array.

Then, if it happens that this is the last item, simply reduce the size of the array by one and adjust the random-number generator range accordingly.

Or if it is not the last item (most likely), copy the very last item into the position of the selected item. And reduce the size of the array and the random-number generator range by one.

Repeat until the array is empty.

(actually, you don't need to change the array size. Simply reducing the range of the random numbers until it is zero will work).
Last edited on
use bool a[4][13]to record what card you choosed and if card is duolicate,choose next card.If next card is duolicate too,choose next card's next card and so on.
Topic archived. No new replies allowed.