| | |
Helping with calling a function
![]() |
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
I wrote this function to generate random numbers and return it to the calling function
but for some reason firstCard and secondCard always have the same value. I am not sure why. Can anyone help me out?
c Syntax (Toggle Plain Text)
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); return total; }
Last edited by ~s.o.s~; Jun 4th, 2007 at 1:20 pm. Reason: Added code tags.
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)
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); return total; }
•
•
Join Date: Jun 2007
Posts: 3
Reputation:
Solved Threads: 0
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?
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.
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:
(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 ...
so it shoots out the same numbers. Remember it's not truly random. Just pseudo-random
C Syntax (Toggle Plain Text)
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 ...
c Syntax (Toggle Plain Text)
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.
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.
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.
![]() |
Similar Threads
- Calling C function from MATLAB (C)
- Still need info on calling a function from bool! (C++)
- How would i recode as a function? (Java)
- Function that returns void (C++)
Other Threads in the C Forum
- Previous Thread: What input reading functions are there in C?
- Next Thread: A chal;lenging debugging problem : NEW
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






