I wrote this function to generate random numbers and return it to the calling function

``````int getCard(void)
{
int range;
int card;

srand(time(NULL));
range = (10 - 1) + 1;

card = rand() % range + 1;

return card;
}

the calling function is....

int first2(void)
{
int firstCard;
int secondCard;
int total;

firstCard  = getCard();
secondCard = getCard();

total      = firstCard + secondCard;

printf("\n\nYour first card is a %d, and your second card is a %d.\n", firstCard, secondCard);
printf("The total for the hand is %d.", total);

}``````

but for some reason firstCard and secondCard always have the same value. I am not sure why. Can anyone help me out?

3
Contributors
5
Replies
6
Views
11 Years
Discussion Span
Last Post by Aia

I think it's to do with where you seed rand. I'm not absolutely certain, but I think (note, this wasn't compiled so it mightn't), this should work -

``````int getCard(void)
{
int range;
int card;

static bool is_set;
if ( !is_set )
{
is_set = true;
srand(time(NULL));
}
else
is_set = false;

range = (10 - 1) + 1;
card = rand() % range + 1;

return card;

}

int first2(void)
{
int firstCard;
int secondCard;
int total;

firstCard = getCard();
secondCard = getCard();

total = firstCard + secondCard;

printf("\n\nYour first card is a %d, and your second card is a %d.\n", firstCard, secondCard);
printf("The total for the hand is %d.", total);

}``````

wow, thank you twomers!! it works. Would you mind explaining a little bit of why you needed to add....

static bool is_set;
if ( !is_set )
{
is_set = true;
srand(time(NULL));
}
else
is_set = false;

and also why it wasn't working the way I had it at first?

Have you ever tried calling rand() without seeding it? If not use a loop and call it ten times. Note the results. Then execute your program again and look at the results. You should notice the same numbers coming up:

``````int main( void )
{
for ( int i=0; i<10; i++ )
{
printf ( "%d ", rand() );
}
return 0;
}``````

(also, could you use code tags? It makes code easier to read.)

I'm not certain, as I said, but I think you're calling the functions quite fast after one another, so perhaps they're getting seeded with the same value ...

``````int main( void )
{
srand( 42 );
for ( int i=0; i<10; i++ )
{
printf ( "%d ", rand() );
}

printf ( "\n" );

srand( 42 );
for ( int i=0; i<10; i++ )
{
printf ( "%d ", rand() );
}
return 0;
}``````

so it shoots out the same numbers. Remember it's not truly random. Just pseudo-random

I just choose the value 42 there. You could use any number and I *think* you should get the same numbers. So I reckon the time(NULL)'s were returning the same number each time.

The static type ... you should do some reading on it. It basically doesn't really go out of scope when the function ends. It kind of globalises local functions within a set scope, in a sense. So the boolean value (is_set), is there. Then I test it as to whether it's true or not. If it isn't I seed rand with one value and then set its value to true. This means for the rest of the
duration of the program it's value is true which means it doesn't get seeded again with the same value.

>and also why it wasn't working the way I had it at first?

Did you include <stdlib.h> and <time.h>, the first time?. Most like you missed <stdlib.h> if I were going to guess.

Take the srand() function out of the getCard().
Call srand( (unsigned) time( NULL ) ) inside main() just one time. That's all you need, not every time that you call the getCard() function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.