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

Recommended Answers

All 4 Replies

Post the code that calls getRand.

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);
   }
}

Here's a hint: What does fork do?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.