random number

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

Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

random number

 
0
  #1
May 1st, 2008
i am trying to generate random numbers in a loop
i wrote this function

  1. int getRand ( int a , int b )
  2. {
  3. static int x = 0 ;
  4.  
  5. if ( x == 0 ) {
  6.  
  7. srand ( time( NULL ) ) ;
  8.  
  9. x++ ;
  10.  
  11. }
  12.  
  13. return a+rand()%(b-a+1);
  14. }

but every time i use it in a loop i get the same number
Last edited by emilio; May 1st, 2008 at 12:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
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: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: random number

 
1
  #2
May 1st, 2008
Post the code that calls getRand.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: random number

 
0
  #3
May 1st, 2008
  1. for(i=0 ; i<5 ; i++)
  2. {
  3. chid = fork();
  4. if (chid == 0)
  5. {
  6. n = getRand(0,9);
  7. printf("child number %d goes to sleep for %d seconds\n",i+1,n);
  8. sleep(n);
  9. kill(getpid(),SIGKILL);
  10. }
  11. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
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: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: random number

 
1
  #4
May 1st, 2008
Here's a hint: What does fork do?
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 146
Reputation: emilio is an unknown quantity at this point 
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster

Re: random number

 
0
  #5
May 1st, 2008
fork creates another process.
but i didnt posted the whole program it's quite long
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 628 | Replies: 4
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC