i'm trying to get random numbers in a loop like this

#include <stdio.h>
#include <time.h>

int getRand ( int a , int b ) 
{	
  return a+rand()%(b-a+1);
}

int main()
{
srand ( time( NULL ) ) ;
int i;

for(i=0 ; i<6 ; i++)
printf("%d\n",getRand(0,9));

return 0;
}

the problem is that i keep getting the same number

Recommended Answers

All 4 Replies

The same number or some numbers are being repeated? Your code works just fine when Edward tests it, but repeated numbers are expected unless you go out of your way to avoid them with something like a random shuffle:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static int *values;
static int index;

int initRandUnique(int lo, int hi)
{
  values = malloc((hi - lo) * sizeof *values);

  if (values == NULL)
    return 0;

  {
    int limit = hi - lo;
    int i;

    for (i = 0; i < limit; ++i)
      values[i] = lo++;

    for (i = 0; i < limit - 1; ++i) {
      int r = i + rand() % (limit - i);
      int temp = values[r];
      values[r] = values[i];
      values[i] = temp;
    }
  }

  return 1;
}

int getRandUnique() 
{
  return values[index++];
}

int main()
{
  int i;

  srand(time(NULL));
  initRandUnique(0, 9);

  for (i = 0; i < 6; ++i)
    printf("%d\n", getRandUnique());

  return 0;
}

ok.
i wanted to use something simple cause my actual program is quite difficult
i need to do something like this:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

 int sig,pid;

int getRand ( int a , int b )
{
return a+rand()%(b-a+1);
}

int main()
 {
      pid = getpid();
      srand ( time( NULL ) ) ;
       int i,n;

     for(i=0 ; i<6 ; i++)
      {
      if(getpid() == pid)
         sig = fork();

      if(sig == 0)
      {
       n = getRand(1,9);
      printf("%d\n",n);
      sleep(n);
      exit(0); 
      }


return 0;
      }

at this point it gives me the same random number.

That's just a fancy way of doing this:

static unsigned seed;

int getRand(int a, int b) 
{
  srand(seed);	
  return a + rand() % (b - a + 1);
}

int main(void)
{
  seed = time(NULL);

  for (i = 0; i < 10; ++i)
    printf("%d\n", getRand(1, 9));

  return 0;
}

You initialize the seed once, and each child process uses the same seed but restarts the sequence from the beginning. In other words, each child process is printing the first number of the sequence, so all of the numbers are going to be the same. You need to reseed rand after spawning a new process:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int sig,pid;

int getRand ( int a , int b )
{
  return a+rand()%(b-a+1);
}

int main()
{
  pid = getpid();

  int i,n;

  for(i=0 ; i<6 ; i++)
  {
    if(getpid() == pid)
      sig = fork();

    if(sig == 0)
    {
      srand ( time( NULL ) );
      n = getRand(1,9);
      printf("%d\n",n);
      sleep(n);
      exit(0); 
    }
  }

  return 0;
}

ok it works
thanks

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.