Hi there, I have a really quick question about a shuffling and dealing cards program I found in a book.
here's main:

// Fig. 8.27: fig08_27.cpp
// Card shuffling and dealing program.
#include "DeckOfCards.h" // DeckOfCards class definition

int main()
{
   DeckOfCards deckOfCards; // create DeckOfCards object
   
   deckOfCards.shuffle(); // shuffle the cards in the deck
   deckOfCards.deal(); // deal the cards in the deck
   return 0; // indicates successful termination
} // end main

Member functions:

// Fig. 8.26: DeckOfCards.cpp
// Member-function definitions for class DeckOfCards that simulates
// the shuffling and dealing of a deck of playing cards.
#include <iostream>
using std::cout;
using std::left;
using std::right;

#include <iomanip>
using std::setw;

#include <cstdlib> // prototypes for rand and srand
using std::rand;
using std::srand;

#include <ctime> // prototype for time
using std::time;

#include "DeckOfCards.h" // DeckOfCards class definition

// DeckOfCards default constructor initializes deck
DeckOfCards::DeckOfCards()
{
   // loop through rows of deck
   for ( int row = 0; row <= 3; row++ )
   {
      // loop through columns of deck for current row
      for ( int column = 0; column <= 12; column++ )
      {
         deck[ row ][ column ] = 0; // initialize slot of deck to 0
      } // end inner for
   } // end outer for
   
   srand( time( 0 ) ); // seed random number generator
} // end DeckOfCards default constructor

// shuffle cards in deck
void DeckOfCards::shuffle()
{
   int row; // represents suit value of card
   int column; // represents face value of card

   // for each of the 52 cards, choose a slot of the deck randomly
   for ( int card = 1; card <= 52; card++ ) 
   {
      do // choose a new random location until unoccupied slot is found
      {
         row = rand() % 4; // randomly select the row (0 to 3)
         column = rand() % 13; // randomly select the column (0 to 12)
      } while( deck[ row ][ column ] != 0 ); // end do...while

      // place card number in chosen slot of deck
      deck[ row ][ column ] = card;
   } // end for
} // end function shuffle

// deal cards in deck
void DeckOfCards::deal()
{
   // initialize suit array
   static const char *suit[ 4 ] = 
      { "Hearts", "Diamonds", "Clubs", "Spades" };

   // initialize face array
   static const char *face[ 13 ] = 
      { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", 
      "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

   // for each of the 52 cards
   for ( int card = 1; card <= 52; card++ )
   {
      // loop through rows of deck
      for ( int row = 0; row <= 3; row++ )
      {
         // loop through columns of deck for current row
         for ( int column = 0; column <= 12; column++ )
         {
            // if slot contains current card, display card
            if ( deck[ row ][ column ] == card ) 
            {
               cout << setw( 5 ) << right << face[ column ] 
                  << " of " << setw( 8 ) << left << suit[ row ]
                  << ( card % 2 == 0 ? '\n' : '\t' );
            } // end if
         } // end innermost for
      } // end inner for
   } // end outer for
} // end function deal

and header file:

// Fig. 8.25: DeckOfCards.h
// Definition of class DeckOfCards that 
// represents a deck of playing cards.

// DeckOfCards class definition
class DeckOfCards
{
public:
   DeckOfCards(); // constructor initializes deck
   void shuffle(); // shuffles cards in deck
   void deal(); // deals cards in deck
private:
   int deck[ 4 ][ 13 ]; // represents deck of cards
}; // end class DeckOfCards

Ok, so I don't quite understand how the

srand()

function is used, (line 34 of main) in combination with the functions

rand()

line 48 and 49. Why do we need srand() in this program, isn't rand() enough?
thanks

srand sets the seed for the random number generator (it takes the number you put in and passes it through a logarithm which generates a pseudo(fake, not totaly random) set of numbers).
setting it by time(), you almost never get the same seed twice and so GREATLY improves randomness.

Ok, so I don't quite understand how the

srand()

function is used, (line 34 of main) in combination with the functions

rand()

line 48 and 49. Why do we need srand() in this program, isn't rand() enough?
thanks

See this and this

Thanks guys, and I read the example WaltP but I am still a little confused. So fundamentally, srand( time( ) ) gets a number from the clock and then it passes it to rand() ?
Like in the card program line 34 gets a random number from the clock (why does it say 0 though?) and then passes it to line 48 and 49. But the number I need need to be respectively smaller than 4 and 13, so what happens if the number that we get from the clock is bigger than those? (I have got the feeling that is a stupid question, so sorry in advance!)
cheers

But the number I need need to be respectively smaller than 4 and 13, so what happens if the number that we get from the clock is bigger than those?

Completely irrelevant. You use srand to seed the random number generator. The current time is typically used because it's constantly changing and as such, gives you a different seed (and subsequently, a completely different random sequence) each time.

rand always returns a number between 0 and RAND_MAX, and you're using arithmetic tricks to shrink that range into your desired 0 to 4 and 0 to 13. So the seed only affects the sequence of random numbers you get from rand, not the range.

saw that Narue posted first but it still adds a bit more info

no. srand sets the seed, it doesnt generate random numbers. the seed is a number that rand uses to generate random numbers.

rand potentially generates large numbers but some maths is all you need to put some boundaries on it:

x = rand() % 10;

the % sign is modulus, it calculates the remainder from the division (divide by 10 in this case). so thinking about it, this means that x could only equal a number from 0 - 9.

about time(0), http://cplusplus.com/reference/clibrary/ctime/time/
time takes an argument which tells it where to store the time, if it is 0, then it returns it.

saw that Narue posted first but it still adds a bit more info

no. srand sets the seed, it doesnt generate random numbers. the seed is a number that rand uses to generate random numbers.

rand potentially generates large numbers but some maths is all you need to put some boundaries on it:

x = rand() % 10;

the % sign is modulus, it calculates the remainder from the division (divide by 10 in this case). so thinking about it, this means that x could only equal a number from 0 - 9.

about time(0), http://cplusplus.com/reference/clibrary/ctime/time/
time takes an argument which tells it where to store the time, if it is 0, then it returns it.

Just note that using the modulus operator works on the low order bits, although in some implementation
they are just as random as the high order ones, but for consistency one should use the high order bits. For example one could
generate a random number from 1-10 as follows.

int lowOrderRand = rand() % 10 + 1 ; // "random" number from 1 - 10 , uses low order bits
int highOrderRand = 1 + int( 10.0 * (rand()/(RAND_MAX+1)) ); // "random" number from 1 - 10, users high order bits

that's great, thank you all, now it is all clear :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.