0

i am trying to generate random numbers in a loop
i wrote this function

int getRand ( int a , int b ) 
{
static int x = 0 ;

if ( x == 0 ) {
 
  srand ( time( NULL ) ) ;

 x++ ;

  }

  return a+rand()%(b-a+1);
}

but every time i use it in a loop i get the same number

2
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by emilio
0
for(i=0 ; i<5 ; i++)
{
chid = fork();
if (chid == 0)
   {
    n = getRand(0,9);
    printf("child number %d goes to sleep for %d seconds\n",i+1,n);
    sleep(n);
    kill(getpid(),SIGKILL);
   }
}
0

fork creates another process.
but i didnt posted the whole program it's quite long

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.