Need C++ BlackJack Shuffle Example

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 2
Reputation: soulbee1 is an unknown quantity at this point 
Solved Threads: 0
soulbee1 soulbee1 is offline Offline
Newbie Poster

Need C++ BlackJack Shuffle Example

 
0
  #1
Apr 15th, 2009
Forgive my ignorance, I am learning. I am writing a C++ BlackJack program for school project. I have googled and incorporated the following code into my project. My results are I am getting a value for the cards of 2 - 5 and i do not understand why.
If someone can advise me of what I am doing wrong or have a good sample for me to look at would be greatly appreciated.
int lowest=2;
int numCards=14;
// 2,3,4,5,6,7,8,9,10,J,Q,K,A
int range=(numCards-lowest)+1;
// range of 1 to 14 card values
srand(time(0)); // initialize seed "randomly"
for(int i = 0; i < (120 - 1); i++)
{
int randNum1 = (rand() % (range));
int randNum2 = (rand() % (range));
card *temp = myDeck[randNum1];
myDeck[randNum1] = myDeck[randNum2];
myDeck[randNum2] = temp;

}
Thanks for your help in advance.
Last edited by soulbee1; Apr 15th, 2009 at 1:55 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,825
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need C++ BlackJack Shuffle Example

 
0
  #2
Apr 15th, 2009
Originally Posted by soulbee1 View Post
Forgive my ignorance, I am learning. I am writing a C++ BlackJack program for school project. I have googled and incorporated the following code into my project. My results are I am getting a value for the cards of 2 - 5 and i do not understand why.
If someone can advise me of what I am doing wrong or have a good sample for me to look at would be greatly appreciated.
int lowest=2;
int numCards=14;
// 2,3,4,5,6,7,8,9,10,J,Q,K,A
int range=(numCards-lowest)+1;
// range of 1 to 14 card values
srand(time(0)); // initialize seed "randomly"
for(int i = 0; i < (120 - 1); i++)
{
int randNum1 = (rand() % (range));
int randNum2 = (rand() % (range));
card *temp = myDeck[randNum1];
myDeck[randNum1] = myDeck[randNum2];
myDeck[randNum2] = temp;

}
Thanks for your help in advance.

I think I'd have to see the rest of the code to be able to duplicate what you are getting exactly. Here are two code snippets I wrote that may be helpful:

http://www.daniweb.com/code/snippet1034.html
http://www.daniweb.com/code/snippet1019.html

One is easier to follow than the other.

My shuffling methods were different from what you are attempting. I'm not 100% sure what is going on with what you are doing (like I said, I'd have to see the rest of the code). Basically you are picking two random indexes and swapping the array elements of those two. That's a valid way of shuffling. I am wondering, though, if there is possibly some issue with your swap. Perhaps it is a shallow versus deep copy issue? Post more of your code so we can see the array initialization and the display function and can test it out.

Here's a way that worked and that looks like it is using the method you are trying to use, so the methodology is sound. Quite possibly the problem may also be somewhere in the code you haven't posted.

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. srand (time (NULL));
  8. int cards[13];
  9. for (int i = 0; i < 13; i++)
  10. cards[i] = i;
  11.  
  12. for (int i = 0; i < 200; i++)
  13. {
  14. int index1 = rand () % 13;
  15. int index2 = rand () % 13;
  16. if (index1 != index2)
  17. {
  18. int temp = cards[index1];
  19. cards[index1] = cards[index2];
  20. cards[index2] = temp;
  21. }
  22. }
  23.  
  24. for (int i = 0; i < 13; i++)
  25. cout << cards[i] << endl;
  26.  
  27. cin.get ();
  28. return 0;
  29. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Need C++ BlackJack Shuffle Example

 
0
  #3
Apr 15th, 2009
Why not use the built-in random shuffle() function from the header <algorithm> ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Need C++ BlackJack Shuffle Example

 
0
  #4
Apr 15th, 2009
Here's a simple shuffle that ensures every element is moved at least once. Note the loop only needs 13 reps for 13 numbers.
  1. const int MAX = 13;
  2. int a[MAX];
  3. for (int i = 0; i < MAX; ++i) a[i] = i;
  4. for (int i = 0; i < MAX; ++i) {
  5. int r = rand() % MAX;
  6. int t = a[i]; a[i] = a[r]; a[r] = t;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Need C++ BlackJack Shuffle Example

 
0
  #5
Apr 15th, 2009
Originally Posted by nucleon View Post
Here's a simple shuffle that ensures every element is moved at least once. Note the loop only needs 13 reps for 13 numbers.
  1. const int MAX = 13;
  2. int a[MAX];
  3. for (int i = 0; i < MAX; ++i) a[i] = i;
  4. for (int i = 0; i < MAX; ++i) {
  5. int r = rand() % MAX;
  6. int t = a[i]; a[i] = a[r]; a[r] = t;
  7. }
You can't ensure that this code will move every element at least once because you can't ensure that r=rand()%MAX; won't give the exact value of i.
The code might also replace twice the same elements, causing no change.
Last edited by unbeatable0; Apr 15th, 2009 at 1:21 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need C++ BlackJack Shuffle Example

 
0
  #6
Apr 15th, 2009
  1. const int MAX = 13;
  2. int a[MAX];
  3. for (int i = 0; i < MAX; ++i) a[i] = i;
  4. for (int i = 0; i < MAX; ++i) {
  5. int r = rand() % MAX;
  6. while(r == i) r = rand() % MAX;
  7. int t = a[i]; a[i] = a[r]; a[r] = t;
  8. }

That oughta take care of the first thing. ;D Shuffling the same elements twice is something that has in chance in real life as well, so it's part of being random.
Last edited by Clockowl; Apr 15th, 2009 at 1:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Need C++ BlackJack Shuffle Example

 
0
  #7
Apr 15th, 2009
Originally Posted by Clockowl View Post
  1. const int MAX = 13;
  2. int a[MAX];
  3. for (int i = 0; i < MAX; ++i) a[i] = i;
  4. for (int i = 0; i < MAX; ++i) {
  5. int r = rand() % MAX;
  6. while(r == i) r = rand() % MAX;
  7. int t = a[i]; a[i] = a[r]; a[r] = t;
  8. }

That oughta take care of the first thing. ;D Shuffling the same elements twice is something that has in chance in real life as well, so it's part of being random.
I didn't say it isn't random, I just said he can't ensure that every element moves at least once
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Need C++ BlackJack Shuffle Example

 
0
  #8
Apr 15th, 2009
Originally Posted by unbeatable0 View Post
I didn't say it isn't random, I just said he can't ensure that every element moves at least once
Well technically, they moved twice. But I catch your drift: it's not guaranteed that all the values will be at different indexes from where they started. Anyway, your first argument was pretty good, nobody would ever "switch" card 1 with card 1, so... I think that's a pretty good algo for randomizing stuff.

But, hey, we're C++, why not use STL's random_shuffle? Works on every STL container, suits the job just fine.

Here's a nice example: http://www.cplusplus.com/reference/a...andom_shuffle/
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Need C++ BlackJack Shuffle Example

 
0
  #9
Apr 15th, 2009
Originally Posted by unbeatable0 View Post
You can't ensure that this code will move every element at least once
Good point. I don't know what I was thinking. An element should be allowed to stay in the same place, otherwise it is certainly not a random permutation.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 395
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is online now Online
Posting Whiz

Re: Need C++ BlackJack Shuffle Example

 
1
  #10
Apr 15th, 2009
Sorry, but most of these solutions are inelegant and that is because to achieve randomness they require an excessive number of random number calls:

(a) Shuffling the stack by selecting two cards and exchanging requires about 7 times the deck of length to get to nearly random on a 52 deck set, and more on a later deck. It is because you can't ensure that you avoid leaving pairs (do this with a deck of 1,2,3,4 ... etc and shuffle and see how many consecuative pairs you have.

(b) If you loop through the deck index in the forward direction and then swap, you definately want the possibility that a card does not move. BUT this shuffle is flawed. Take a deck of 26 0 + 26 1 in that order ie. 0,0,0,0.....,0,1,1...1 and then shuffle. If you do the shuffle once and then add the first 5 cards you get about 2.55 and 2.45 for the sums as you repeat to an infinite number of tests. [i.e. shuffle the deck, sum, reset the deck] * infinity. That is a significant fraction for a casino to lose. The drift to pure randomness is asymptotic.

(c) Use the algorithm shuffle. Depends on implementation but normally very good

(d) something that is random is this: [ well significantly better than either (a) or (b) ]

  1. int deckIndex[52];
  2. int deck[52];
  3. for(int i=0;i<52;i++)
  4. deckIndex[i]=i;
  5.  
  6. for(int i=52;i>=1;i--)
  7. {
  8. // ugly: Use a proper random number system e.g
  9. // Mesene Twister.
  10. const int X=rand() % i;
  11. deck[i-1]=deckIndex[X];
  12. deckIndex[X]=deckIndex[i-1];
  13. }

Yes you have to use another array, but it is a temp, the calls to your random number generator are expensive. If you are doing many shuffles then you don't need to regenerate deckIndex as any list works. [you can cut one off the calls to the rng. but I haven't]

Note, I had look at random_shuffle under gcc/stl and it looks like a sophisticated version of (d) -- but I am not 100% certain of that.
Last edited by StuXYZ; Apr 15th, 2009 at 5:35 pm.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC