Making a blackjack game. How to have two different random variables?(one for dealer one for user)

I am trying to make a blackjack game, where the user gets a pair of cards, and the dealer gets a pair of cards. My problem is that right now they are getting the same pair of cards, and I don't know how to change the random number generator. Here is what I have:

1
2
3
4
5
6

srand(time(0));

card1 = (rand()%13) + 2;
card2 = (rand()%13) + 2;


I have that for both the dealer and the user. I understand that it will give me the same random numbers, but how can I generate a different number for the user? I cannot put (1) in where the 0 is (which is what I thought would do it).

Is there a better way to use random numbers?

Edit: Side note. I don't know if this matters, but about 10% of the time when I run it, the complier gives me some random symbol instead of a number between 2-14. I don't know if this is something to do with my other code, or this code.
Last edited on
rand() generates a different random number every time. I believe it seeds itself using the last number generated. Your code should be fine, so you must have a problem elsewhere.

Do note that rand() isn't generally considered a very good RNG. It simply generates a predictable "random" series of numbers based on the seed. If you pass it the same seed you'll get the same series.

PSEUDO EDIT: If you call srand(time(0)) both times then you will initialize the generator with the second you started the program at again, causing the start of the same sequence. Only call srand once, at the beginning of your program.

ACTUAL EDIT: I guess I had time figured wrong all this time. Good thing I never used it :P The argument you pass is a pointer to a time object which the function uses to store the current time value. Although the thing about calling srand only once still stands.

And your error probably still comes from the fact that you call srand twice, (if this is what you do) since those sections of code most probably likely definitely take less than 1 second to execute.
Last edited on
Those 2 statements should generate 2 different random numbers most of the time. (Seeing as your srand is set as a random number).

Posting slightly more of your code might show why they aren't acting correctly.
Do srand(time(NULL)); otherwise, you are (in a way) telling it to generate a random number from 0.

- Kyle
NULL is defined as 0.
Topic archived. No new replies allowed.