# Small problem with rand()

• 05-19-2010
XunTric
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.

• 05-19-2010
GReaper
use srand to seed the random generator like this:
Code:

srand(time(NULL))
• 05-19-2010
XunTric
Quote:

Originally Posted by Sipher
use srand to seed the random generator like this:
Code:

srand(time(NULL))

Thanks a lot.
• 05-19-2010
GReaper
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 )
• 05-20-2010
msh
Quote:

Originally Posted by Sipher
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.
• 05-21-2010
GReaper
Well, what if you want to print the name of the card you just drew?