Thread: Small problem with rand()

  1. #1
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90

    Small problem with rand()

    I made a small blackjack game as one of the programs I am going to demonstrate to my teacher at the end of the year.
    I am simply using a string with all the 52 cards, like this:
    Code:
    // 0 represents 10
    string cardstock = "234567890JQKA234567890JQKA234567890JQKA234567890JQKA";
    Then I am using rand() to pull out one random char from that string every time either you or the dealer picks a card.

    This is working out alright, and the game works, however there is one slight problem.

    The game pulls out the same cards every time you start the program.
    In my case, your first hand will always be "J + 6".
    The problem obviously lies in rand().
    So how to I fix this so you don't always start with the same cards?
    It doesn't have to to be a "true random" generator.
    As long as you don't always start with the same hand I'm happy with it.

    Thanks in advance.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    use srand to seed the random generator like this:
    Code:
    srand(time(NULL))
    Devoted my life to programming...

  3. #3
    Pawn, Pascal and C++
    Join Date
    Sep 2005
    Posts
    90
    Quote Originally Posted by Sipher View Post
    use srand to seed the random generator like this:
    Code:
    srand(time(NULL))
    Thanks a lot.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    But i don't think your cards-in-a-string implementation is correct, because there are 4 types of cards, remember?

    You'll have to store their color ( black, red ) and their type ( Spades, Hearts, Diamonds, Clubs )
    Devoted my life to programming...

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Sipher View Post
    But i don't think your cards-in-a-string implementation is correct, because there are 4 types of cards, remember?

    You'll have to store their color ( black, red ) and their type ( Spades, Hearts, Diamonds, Clubs )
    I don't think suit (type) or color are important for your basic game of Blackjack.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, what if you want to print the name of the card you just drew?
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  2. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  3. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  4. a very small problem, i'm new... please help?
    By Uberverse in forum C++ Programming
    Replies: 9
    Last Post: 11-10-2007, 10:44 AM
  5. Small problem I need help with.
    By ajdspud in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2005, 05:43 PM