Helping with calling a function

Reply

Join Date: Jun 2007
Posts: 3
Reputation: aznstyles408 is an unknown quantity at this point 
Solved Threads: 0
aznstyles408 aznstyles408 is offline Offline
Newbie Poster

Helping with calling a function

 
0
  #1
Jun 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,857
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 53
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Helping with calling a function

 
0
  #2
Jun 4th, 2007
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 -

  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. }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 3
Reputation: aznstyles408 is an unknown quantity at this point 
Solved Threads: 0
aznstyles408 aznstyles408 is offline Offline
Newbie Poster

Re: Helping with calling a function

 
0
  #3
Jun 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,857
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 53
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Helping with calling a function

 
0
  #4
Jun 4th, 2007
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
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,857
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 53
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Helping with calling a function

 
0
  #5
Jun 4th, 2007
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,028
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 175
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Helping with calling a function

 
0
  #6
Jun 4th, 2007
>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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC