I'm trying to make a shuffler for a blackjack program it's supposed to give me back a random number from a deck of cards each time i call it. But it's also supposed to keep track of cards out of the deck..

I need to make it a function call, but before i do that it's not even working in the first place... it's giving me 51 random crazy numbers..

help please?


#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

//int random();
int main()
{//begin main
srand(time(0));
int deck[52];
//int random()
//{
for (int i=0; i<51; i++)
{
int j = rand() % (51 - i) + i + 1;
int t = deck[i];
deck[i] = deck[j];
deck[j] = t;
//return t;
//}
cout << t << endl;
}   
//randomize the time

//int cardval = random();


 system("pause");


 return 0;
}//end main

Before you can shuffle the deck, you need to assign values to it. Maybe something like:

int i;
for( i = 0; i < 52; i++ )
   deck[i] = i+1;  //assuming you want values 1-52

Now that you have some values, shuffling makes sense.

yea... i'm so stupid.. Thanks for that one vmanes.

BTW could you suggest how i should deal out the cards, i need 2 cards for human 2 for computer to start. So should i do a function call to my shuffler to generate 4 random cards or?

You trying to cheat? Once the deck is shuffled, just deal cards starting at the beginning. Index 0 to the human, 1 to dealer, 2 to human, 3 to dealer. Keep track of where you are in the deck as player and dealer take hits.

Hope you don't mind an additional comment...
If you shuffle the whole deck, you spend an amazing amount of time in the random number generator. It is a lot better (particularly for blackjack simulations -- where you only have a few cards to get out.), to generate the random cards on the fly. The is important when considering the large deck (normally 8) that are used.

Additional, to that blackjack can be played with a reduced deck, i.e. the suits don't matter. So a 64 bit random number can be used to generate a number of cards.

Anyway, you need (for a one pack suited game):

for(int i=0;i<52;i++)
  deck[i] = i+1; 

int cardLeft=52;
int *cardPtr=deck;
for(int i=0;i<cardNeeded;i++)
{
   int j=rand() % cardLeft;
   // swap first card with selection
   // use : std::swap(cardPtr,cardPtr+j) ;  or

   // swap cards
   int tmp=cardPtr[j];
   cardPtr[j]=*cardPtr;
   *cardPtr=tmp;
  // Increment top of deck.
   cardPtr++;
 }

Now you can take the top of the deck and use it. That can be put into a function form if required. It doesn't really matter how you write this loop, or you defer it as you need each card. The key idea is to reduce the number of very expensive calls to the random number generator.

p.s. You don't use rand() for any accurate simulation work. (e.g. use Mersenne Twister instead.)

Hey stu appreciate the input, but you're code is a little bit over my head. I'm just getting into pointers so i will use it as a reference :)

BTW on my current blackjack program im having trouble resetting the hand after each game, as in the game is over computer won with blackjack, how do i clear hands and reshuffle the deck and redeal.?

Keep it simpler, it's C++ now and here: ;)

#include <ctime>
#include <cstdlib>
#include <algorithm>
...
srand((unsigned)time(0));
random_shuffle(deck,deck+52);
// All done ;)
Comments
Short, sweet, and to the point.
Thanks, I needed the reminder.... (getting old :-)

ArkM comment is good. It just works.

It does completely shuffle the deck, and it avoids all the pitfall on non-random shuffles [D. E. Knuth, The Art of Computer Programming vol2]. It is near perfect until you get a CPU bottle neck and it can be called with any RandomNumber Generator (add a third argument to the call).

However, before you go on and try to write this program, PLEASE go an read about pointers. It will take you less than a day, ArkM's solution is using pointers. your clearing the deck before a new game is 99% likely to be a pointer error (and if it is not you are likely to be using a very inefficient algorithm). After pointers, you will enjoy your C++ code a lot more, particularly this type of problem, and have the basis for understanding stl classes like list/vector etc.

p.s. If you get stuck, post a question with a bit of test code.

This article has been dead for over six months. Start a new discussion instead.