# Deck Shuffle

This is a discussion on Deck Shuffle within the C++ Programming forums, part of the General Programming Boards category; I find the it interesting that you want to simulate the shuffle of a real dealer. But conjoining a split ...

1. I find the it interesting that you want to simulate the shuffle of a real dealer.
But conjoining a split deck rarely ever is a linear one to one swap of cards.
Usually you get say 0-4 from one side maybe and 0-4 from one side.
So I just wanted to say it would be interesting if you allowed for this varience in your code.
What would be really intersting is to create a greater or lesser variance based on the skill level of your bot dealer.

(me hides for being lazy)

2. I do allow for the variance in my code. Look at my explination of what I wanted it to do when i first posted. Then look at the code. I use rand() to get a number between 1-4, then use that number as the variance. The only thing that isnt real is that the variance is always the sameeach time the deck is shuffled.

Also, the dealer skill is an interesting idea. But, I want to shuffle the deck as best as possible.

3. My bad, I guess if I wasn't lazy I would have analyzed the code deeper and seen this.
kudos to you

4. >> I want to shuffle the deck as best as possible.
As long as you are using rand, a single call to random_shuffle will shuffle the deck as well as possible. Your version is just an interesting simulation of human shuffling, it is not more random (and is potentially less random, I'm not sure).

5. Originally Posted by Daved
As long as you are using rand, a single call to random_shuffle will shuffle the deck as well as possible. Your version is just an interesting simulation of human shuffling, it is not more random (and is potentially less random, I'm not sure).
Yeah I was thinking about going with random_shuffle when I found out about it, even tried to use it as you saw in my code(all the other code was commented out when I tried), but that seemed harder because I got that RandomAccessInteger error. But it's also hard for me to throw away that code seeing as I wrote it and tried to debug it for an hour or two maybe more before I posted here.

Originally Posted by Daved
>> But that didn't work because the array I pass isnt of type RandomAccessIterator or something.

Sorry, it should be random_shuffle(d, d+52). If you just want to get a real shuffle, calling that once is all you have to do.
Use that and it should work!

7. Yeah I think I'll give it a try but I am still undecided about which I want to use. I might use random_shuffle, but it's so hard to just throw away that code I worked on for hours.

8. I might use random_shuffle, but it's so hard to just throw away that code I worked on for hours.
Well, you might be throwing that code away (for now), but not what you learnt from writing it and asking for help concerning it.

9. This is true laserlight. I have learn the valuable lesson of keeping things in the smallest scope possible and when I do loops make sure I reset things that need to be reset.

10. Don't "Throwaway" the code in fact you can improve it further by considering more meaninful names for your variables. This will benifit others reading the code, and ultimately make you a better programmer.

11. Here's an example using random_shuffle():
Code:
```#include <iostream>
#include <algorithm> //random_shuffle()
#include <ctime> //time()
#include <cstdlib> //srand()

using namespace std;

int main()
{
srand(time(0)); //seed random number generator
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for(int j = 0; j<10; j++)
{
random_shuffle(arr, arr+9);

for(int i=0; i<9; i++)
{
cout<<arr[i];
}

cout<<endl;
}

cin.get();
return 0;
}```

12. Originally Posted by 7stud
Here's an example using random_shuffle():
Code:
```#include <iostream>
#include <algorithm> //random_shuffle()
#include <ctime> //time()
#include <cstdlib> //srand()

using namespace std;

int main()
{
srand(time(0)); //seed random number generator
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

for(int j = 0; j<10; j++)
{
random_shuffle(arr, arr+9);

for(int i=0; i<9; i++)
{
cout<<arr[i];
}

cout<<endl;
}

cin.get();
return 0;
}```
That is all good and dandy. But I just ran your example and I got the same set of numbers each time a ran it, which was like 10 times. So, that basically convinced me not to use it unless someone can help with that.

13. Works for me, if I do
\$ ./a.out ; sleep 2 ; ./a.out

But if I do this, then yes the results are the same, basically because time hasn't gone anywhere and that's what you pass to srand()
\$ ./a.out ; ./a.out

14. well with the way he uses srand(0) what part of time is being used?. And the way I did it was:
\$./a.out
\$./a.out

and I did that 10 times and it was the same set of numbers. And I just did it again, however many minutes after the fact and bang the same numbers.

15. > well with the way he uses srand(0)
Ok, so you can't read then, if you've got srand(0);
>> srand(time(0)); //seed random number generator

Page 2 of 4 First 1234 Last