944,033 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1548
  • C RSS
Jun 4th, 2007
0

Helping with calling a function

Expand Post »
I wrote this function to generate random numbers and return it to the calling function
  1. int getCard(void)
  2. {
  3. int range;
  4. int card;
  5.  
  6. srand(time(NULL));
  7. range = (10 - 1) + 1;
  8.  
  9. card = rand() % range + 1;
  10.  
  11. return card;
  12. }
  13.  
  14. the calling function is....
  15.  
  16. int first2(void)
  17. {
  18. int firstCard;
  19. int secondCard;
  20. int total;
  21.  
  22. firstCard = getCard();
  23. secondCard = getCard();
  24.  
  25. total = firstCard + secondCard;
  26.  
  27. printf("\n\nYour first card is a %d, and your second card is a %d.\n", firstCard, secondCard);
  28. printf("The total for the hand is %d.", total);
  29.  
  30. return total;
  31. }
but for some reason firstCard and secondCard always have the same value. I am not sure why. Can anyone help me out?
Last edited by ~s.o.s~; Jun 4th, 2007 at 1:20 pm. Reason: Added code tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aznstyles408 is offline Offline
3 posts
since Jun 2007
Jun 4th, 2007
0

Re: Helping with calling a function

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 -

C++ Syntax (Toggle Plain Text)
  1. int getCard(void)
  2. {
  3. int range;
  4. int card;
  5.  
  6. static bool is_set;
  7. if ( !is_set )
  8. {
  9. is_set = true;
  10. srand(time(NULL));
  11. }
  12. else
  13. is_set = false;
  14.  
  15. range = (10 - 1) + 1;
  16. card = rand() % range + 1;
  17.  
  18. return card;
  19.  
  20.  
  21. }
  22.  
  23.  
  24. int first2(void)
  25. {
  26. int firstCard;
  27. int secondCard;
  28. int total;
  29.  
  30. firstCard = getCard();
  31. secondCard = getCard();
  32.  
  33. total = firstCard + secondCard;
  34.  
  35. printf("\n\nYour first card is a %d, and your second card is a %d.\n", firstCard, secondCard);
  36. printf("The total for the hand is %d.", total);
  37.  
  38. return total;
  39. }
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jun 4th, 2007
0

Re: Helping with calling a function

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?
Last edited by aznstyles408; Jun 4th, 2007 at 9:13 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aznstyles408 is offline Offline
3 posts
since Jun 2007
Jun 4th, 2007
0

Re: Helping with calling a function

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:

  1. int main( void )
  2. {
  3. for ( int i=0; i<10; i++ )
  4. {
  5. printf ( "%d ", rand() );
  6. }
  7. return 0;
  8. }

(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 ...

  1. int main( void )
  2. {
  3. srand( 42 );
  4. for ( int i=0; i<10; i++ )
  5. {
  6. printf ( "%d ", rand() );
  7. }
  8.  
  9. printf ( "\n" );
  10.  
  11. srand( 42 );
  12. for ( int i=0; i<10; i++ )
  13. {
  14. printf ( "%d ", rand() );
  15. }
  16. return 0;
  17. }

so it shoots out the same numbers. Remember it's not truly random. Just pseudo-random
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jun 4th, 2007
0

Re: Helping with calling a function

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jun 4th, 2007
0

Re: Helping with calling a function

>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.
Last edited by Aia; Jun 4th, 2007 at 7:34 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: What input reading functions are there in C?
Next Thread in C Forum Timeline: A chal;lenging debugging problem : NEW





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC