Shuffle function doesn't work

I have an array which creates a deck of cards from 1-52. The array is numbered so it would be: card[0]=1,card[1]=2 etc.

Below is my random function and I keep getting repeat numbers.

For example it would list the numbers from beginning to end: 5, 10, 12, 49, 49, 14 etc.

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
	random_deck()
	{
		int r_card1;
		int r_card2;
		int temp1;
		int temp2;

		

		srand(time(0));
		for(int i=0;i<100;++i)
		{
			r_card1 = rand() % 51;
			r_card2 = rand() % 51;
			while(r_card1 == r_card2)
			{
				r_card2 = rand() % 51;
			}

			
			temp1 = r_card1;
			temp2 = r_card2;

			card[r_card1] = card[temp2];
			card[r_card2] = card[temp1];
		}
	}
Last edited on
On lines 24/25 you do not actually swap the cards. You are basically doing this:

1
2
card[1] = card[2]; //overwrite card 1
card[2] = card[1]; //does nothing, since the previous op made card[1] == card[2] 

You need a temporary variable in between.
1
2
3
temp = var1;
var1 = var2;
var2 = temp;

Or you could just call std::random_shuffle.
http://www.cplusplus.com/reference/algorithm/random_shuffle/
Firedraco, could you elaborate on how the cards aren't swapping? I used temp variables to swap the cards.

tummychow, the random_shuffle seems like it will work, but I can't get it to work in my OOP program.

1
2
3
temp = var1;
var1 = var2;
var2 = temp;


Which variables did mean to swap?

Sorry, I've been stuck on this problem for a few days now. I can't seem to fix somethign as simpel as this.
The code snippet I gave you swaps var1 and var2, through an arbitrary variable temp.
Ok so I tried what you suggested:

1
2
3
4
5
6
temp1 = r_card1;
r_card1 = r_card2;
r_card2 = temp1;

decks[r_card1] = decks[r_card2];
decks[r_card2] = decks[r_card1];


I still get repeat values.
Last edited on
Got it to work!!!!

Such a stupid mistake I made.

So I misunderstood what you were doing and re-did it:

1
2
3
temp_deck = decks[r_card1];
decks[r_card1] = decks[r_card2];
decks[r_card2] = temp_deck;


It's working. Thank you very much!
here a hint build a deck what you want in it. Then shuffle it.
this should help you.
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

// if you want your cards to have more shape then numbers
enum Suit { Clubs , Spades , Diamonds , Hearts };
class Card_Info
{
  public:
	int value;
	std::string face;
	Suit suit;
};

void Swap( int &a , int &b )
{ int c = a; a = b; b = c; }

void Shuffle ( int Deck2[] , int size )
{
	int i, r;
	for (i = 0; i < size; i++)
	{
		r = rand()%size;
		Swap( Deck2[i] , Deck2[r] );
	}
}

int main()
{
	srand((unsigned)time(0)); 
	int Deck[52] , i;
	
	// build the deck
	for (i = 0; i < 52; i++)
	{
		Deck[i] = i + 1;
	}
	
	// shuffle the deck
	Shuffle( Deck , 52 );
	
	// print shuffle deck
	for (i = 0; i < 52; i++)
	{
		std::cout << Deck[i] << "\n";
	}
	
	return 0;
}
Topic archived. No new replies allowed.