Getting a random number

This is a discussion on Getting a random number within the C++ Programming forums, part of the General Programming Boards category; Hi. I posted yesterday, but didn't get a good enough idea of how to proceed. I have four classes in ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Getting a random number

    Hi. I posted yesterday, but didn't get a good enough idea of how to proceed. I have four classes in a card game: Card, Deck, Player, and Game. The Card class is as follows:
    Code:
    class Card
    {
      public:
        Card();
        void setValue(int);
        int getValue();
      private:
        int value;
    };
    Class Deck should not have a private section, or any data members.
    Code:
    class Deck
    {
      public:
        Deck();
        Card randomCard;
        int shuffle();
    };
    The code for the last two classes is:
    Code:
    class Player
    {
      public:
        Player();
        void enterName();
        void setName(string);
        string getName();
        int getTotal();
        int addCardValue(Card);
      private:
        string name;
        int total;
    };
    
    class Game
    {
      public:
        void enterPlayerInfo();
        void playGame();
        Player getWinner();
        void showGame(Player);
      private:
        void drawPlayer();
        void drawDealers();
        Player user;
        Player computer;
    };
    There are a few things that I am having difficulty with. As someone suggested yesterday (I haven't posted to forums such as these for a while), I try to ask one question at a time.

    I'm not sure how to get a new randomly drawn card to show up.
    I am given the following to put into the Main function:
    Code:
    int main()
    {
      srand( (unsigned)time( NULL ) ); 
      return 0;
    }
    My shuffle() member function looks like this:
    Code:
    int Deck::shuffle()
    {
      return rand() % 10;
    }
    Now, I am not sure where to go from here. I was thinking of trying to incorporate the accessor functions in Card, to set a new number each time. I'm puzzled though. Thanks in advance for any suggestions. Steve

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Class Deck should not have a private section, or any data members.
    Why not?
    I would think a deck would consist of 52 cards

    Shuffling the deck means something like
    swap( deck[i], deck[j] );
    a number of times

    And your Game class should consist of two Players and one Deck
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    From your previous post

    Your assignment instructions give you some pretty strange requirments.

    In your first post your assignment states that you only use cards 2 through 9, (pretty strange game of blackjack if you ask me) and that the shuffle is just the assignment of a random number well if you just get a random number between 0 and 7 you can add 2 to it to keep it in your range. but it is a requirement that you get a number between 0 and 9 oh well. just put in a comparision for if less then 2 get another number.

    from your assignment instructions
    // class Deck
    // Encapsulates a deck of cards.
    // Functionality (or Interface):
    // 1. Returns a randomly drawn Card object
    // 2. Shuffles the deck (for this assignment, this functionality
    // is defined as simply setting the random number generator)
    // Hint: Deck should not have a private section, or any data members.
    Last edited by manofsteel972; 04-12-2004 at 07:15 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by smitsky
    Hi. I posted yesterday, but didn't get a good enough idea of how to proceed.
    Don't start a new thread for the same topic. It's permissable to replay to the same thread (and in fact is highly suggested)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. random number tutorial
    By 7stud in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:41 PM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21