Good webpage that explains how to use rand() and srand() correctly?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 82
Reputation: riahc3 is an unknown quantity at this point 
Solved Threads: 0
riahc3 riahc3 is offline Offline
Junior Poster in Training

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #11
Dec 1st, 2008
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:
  1. void main()
  2. {
  3. int num;
  4. srand(time(0));
  5. int=numbergenerator;
  6. }
  7.  
  8. int numbergenerator()
  9. {
  10. int num;
  11. 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
  12. do
  13. {
  14. num=rand() % 4;
  15. printf ("number %i",num);
  16. getch(c);
  17. 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
  18. return num;
  19. }

running srand(time(0)) each time

  1. void main()
  2. {
  3. int num;
  4. int=numbergenerator;
  5. }
  6.  
  7. int numbergenerator()
  8. {
  9. int num;
  10. 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
  11. srand(time(0))
  12. do
  13. {
  14. num=rand() % 4;
  15. printf ("number %i",num);
  16. getch(c);
  17. 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
  18. return num;
  19. }
Last edited by riahc3; Dec 1st, 2008 at 4:57 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #12
Dec 1st, 2008
Originally Posted by riahc3 View Post
Again this is for C++, not C.
I did not realize that you were unable to convert a command as cout to a command as printf .....

Originally Posted by riahc3 View Post
It is obvious that you are a complete *******; Some are just starting out or simply need help to understand a certain area
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.
Originally Posted by riahc3 View Post
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....
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.
Last edited by niek_e; Dec 1st, 2008 at 4:59 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #13
Dec 1st, 2008
Originally Posted by riahc3 View Post
One last question: Do I have to run srand(time(0)); only once in the entire program
Yes

[edit]
You might want to read this about using void main()
Last edited by niek_e; Dec 1st, 2008 at 5:00 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Good webpage that explains how to use rand() and srand() correctly?

 
0
  #14
Dec 1st, 2008
>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++.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC