943,871 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1755
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 15th, 2009
0

Need C++ BlackJack Shuffle Example

Expand 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.
Last edited by soulbee1; Apr 15th, 2009 at 1:55 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
soulbee1 is offline Offline
2 posts
since Apr 2009
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

Click to Expand / Collapse  Quote originally posted by soulbee1 ...
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.

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

Why not use the built-in random shuffle() function from the header <algorithm> ?
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

Here's a simple shuffle that ensures every element is moved at least once. Note the loop only needs 13 reps for 13 numbers.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

Click to Expand / Collapse  Quote originally posted by nucleon ...
Here's a simple shuffle that ensures every element is moved at least once. Note the loop only needs 13 reps for 13 numbers.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

Click to Expand / Collapse  Quote originally posted by Clockowl ...
  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
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

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/
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 15th, 2009
0

Re: Need C++ BlackJack Shuffle Example

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.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 15th, 2009
1

Re: Need C++ BlackJack Shuffle Example

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) ]

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help writing script
Next Thread in C++ Forum Timeline: Need code for header and program C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC