shuffling a vector

Pages: 12
hey guys, i am at a loss about how to simply swap the contents of a vector to give a 'random shuffle' do you guys have any examples or ideas that may work?
std::random_shuffle(vec.begin(),vec.end());
Implementation is here: http://www.cplusplus.com/reference/algorithm/random_shuffle/
i dont quite understand how that works, saying i had a vector called 'vec' how would i be able to create 1 function for shuffling?
The function already exists, so just call it.
so what you wrote up there is a function?
it doesnt really explain how it is working though
...
Yes, it does. The implementation is right on that page:

for (i=n-1; i>0; --i) swap (first[i],first[rand(i+1)]);

And the explanation is right there too:
The function swaps the value of each element with that of some other randomly chosen element. When provided, the function rand chooses which element.
Last edited on
ah ok, i see now, but i just need it all to fit in one function in a class, the example here shows more than one

im having abit of trouble applying it to my vector
Last edited on
shows more than one

Shows more than one what?
function

sorry, i haven't been too clear here, i am trying to shuffle a deck of cards, i need the function to shuffle the vector 'pcardvector'

something like this:

1
2
3
4
5
6
void shufflecards ()
            {
                srand ( time(NULL) );

                random_shuffle(pcardvector.begin(),pcardvector.end());
            }


just not sure how to implement it
Last edited on
just not sure how to implement it

You already did?
Except that the srand call belongs to the beginning of main.
but it isnt shuffling, i have a function that prints the vector, and after using the 'shuffle function, it stays the same
It doesn't (except by pure chance). Show your full code.
my main:

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include "class.cpp"
#include "deck class.cpp"

using namespace std;

vector<Card> deck1;
/** this void function creates a vector of 'card' objects using the push_back
     function*/
void createDeck()
{
    int n,i,j;
    string b;
    j = 0;

    for(n = 0; n < 4; n++)
    {
        for(i = 1; i < 14; ++i)
        {
            switch (n)
            {
                case 0: b = "Hearts";
                break;

                case 1: b = "Spades";
                break;

                case 2: b = "Diamonds";
                break;

                case 3: b = "Clubs";
                break;
            };

            deck1.push_back(Card(i, b));

        }
    }

}

/** i have placed the above created deck as the argument for the deck class
    constructor, and called for the printdeck function to be used.*/
int main()
{
    srand ( time(NULL) );
    createDeck();
    Deck(deck1).printdeck();
    Deck(deck1).shufflecards();
    Deck(deck1).printdeck();
    return 0;
}


my deck class:

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

using namespace std;

class Deck
{
        /** these private vectors are the deck, and the pointers to the deck */
    private: vector<Card> cardvector; vector<Card *> pcardvector;

    public:
    /** this constructor also uses a for loop to create a pointer for each card */
            Deck (vector<Card> cardvector2)
            {
                cardvector = cardvector2;

                for (size_t x = 0; x < cardvector.size(); ++x)
                {
                    pcardvector.push_back(&cardvector[x]);
                }
            }
/** by printing with the pointers, it allows me to shuffle the pointers later on
rather than the actual deck, this is good because i still have my original deck.
 */
            void printdeck ()
            {
                for (size_t x = 0; x < pcardvector.size(); ++x)
                {
                    switch(pcardvector[x]->getvalue())
                    {
                        case 1: cout << "Ace"; break;

                        case 11: cout << "Jack"; break;

                        case 12: cout << "Queen"; break;

                        case 13: cout << "King"; break;

                        default: cout << pcardvector[x]->getvalue(); break;
                    }
                    cout << " of ";
                    cout << pcardvector[x]->getsuit() << endl;

                }cout << endl;
            }

            void nextcard ()
            {

            }

            void shufflecards ()
            {
                random_shuffle(pcardvector.begin(),pcardvector.end());
            }
};


my card class:

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

 using namespace std;

 class Card
{
    private: string suit; int value;

    public:
            Card()//default constructor (no arguments) allows you to make an
            {     //array
                value = 0;
            }

            Card(int v, string s)// allows the private attributes to be set
            {
                value = v;
                suit = s;
            }

            int getvalue()// returns the cards value
            {
                return value;
            }

            string getsuit()// returns the cards suit
            {
                return suit;
            }
};
1
2
3
Deck(deck1).printdeck();
Deck(deck1).shufflecards();
Deck(deck1).printdeck();

You create a temporary copy of deck1, print it, then you create another temporary copy of deck1 and shuffle it, then you create another temporary copy of deck1 and print it.

Edit: make that "a temporary instance of Deck that copies the deck1 vector" in all three cases.
Last edited on
how would i get past that?? also, how are they temporary?
Deck(deck1)

This creates a new object of type Deck, which will be a copy of deck1. It's temporary because you've not given it a name.

If you want to shuffle the actual deck1, try

deck1.shufflecards();
isnt its name deck1? and that wont work cause there is no shufflecards in the card class
deck1 is not a Deck so that will not work.

First create a named Deck object
Deck deck(deck1);
You can then call the functions like this:
deck.shufflecards();

Pages: 12