| | |
Good webpage that explains how to use rand() and srand() correctly?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
I figured it out
Had to run the seed outside of the do-while loop. Worked great.
One last question: Do I have to run srand(time(0)); only once in the entire program or each time I run a function called "int numbergenerator" which returns the random number?
Ill explain with code
running srand(time(0)); once:
running srand(time(0)) each time
Had to run the seed outside of the do-while loop. Worked great.One last question: Do I have to run srand(time(0)); only once in the entire program or each time I run a function called "int numbergenerator" which returns the random number?
Ill explain with code
running srand(time(0)); once:
C Syntax (Toggle Plain Text)
void main() { int num; srand(time(0)); int=numbergenerator; } int numbergenerator() { int num; char c; //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE TIMES AS A TEST do { num=rand() % 4; printf ("number %i",num); getch(c); while (c!="x"); //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE AS A TEST return num; }
running srand(time(0)) each time
C Syntax (Toggle Plain Text)
void main() { int num; int=numbergenerator; } int numbergenerator() { int num; char c; //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE TIMES AS A TEST srand(time(0)) do { num=rand() % 4; printf ("number %i",num); getch(c); while (c!="x"); //DO NOT PAY ATTENTION TO THIS. IT IS JUST SO THE DO-LOOP DOESN'T END AND GENERATES INFINITE AS A TEST return num; }
Last edited by riahc3; Dec 1st, 2008 at 4:57 am.
I did not realize that you were unable to convert a command as
I won't start a flamewar about the " ******* ".
If you're really trying to understand 'a certain area', you should really read the link I posted. I wasn't kidding, it's good info although there might be some C++ code involved.
So you have an error in your code/logic. Post your code because I don't have a magic crystal ball and I can't se your code from here.
cout to a command as printf .....•
•
•
•
It is obvious that you are a complete *******; Some are just starting out or simply need help to understand a certain area
If you're really trying to understand 'a certain area', you should really read the link I posted. I wasn't kidding, it's good info although there might be some C++ code involved.
•
•
•
•
Thank you for posting this code. It works but my only problem is that (in a do-while loop) it generates 0 about 10-15 times then 3 another 10-15 times then 3 another 10-15 times then 2 another 10-15 times then 1 another 10-15 times then 0 again and all over again....
Last edited by niek_e; Dec 1st, 2008 at 4:59 am.
•
•
•
•
One last question: Do I have to run srand(time(0)); only once in the entire program
[edit]
You might want to read this about using void main()
Last edited by niek_e; Dec 1st, 2008 at 5:00 am.
>I figured it out
It seems miracles do happen. Though through all your bitching and moaning about C++, you still don't seem to understand that srand and rand are used identically in both languages. They're a part of the C standard library and the C subset of C++.
It seems miracles do happen. Though through all your bitching and moaning about C++, you still don't seem to understand that srand and rand are used identically in both languages. They're a part of the C standard library and the C subset of C++.
New members chased away this month: 4
![]() |
Other Threads in the C Forum
- Previous Thread: deleting special characters from string of numbers
- Next Thread: Arrays and Pointers
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






