User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,705 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,203 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 379 | Replies: 4 | Solved
Reply
Join Date: Nov 2007
Posts: 73
Reputation: emilio is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster in Training

random number

  #1  
May 12th, 2008
i'm trying to get random numbers in a loop like this

  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. int getRand ( int a , int b )
  5. {
  6. return a+rand()%(b-a+1);
  7. }
  8.  
  9. int main()
  10. {
  11. srand ( time( NULL ) ) ;
  12. int i;
  13.  
  14. for(i=0 ; i<6 ; i++)
  15. printf("%d\n",getRand(0,9));
  16.  
  17. return 0;
  18. }

the problem is that i keep getting the same number
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 345
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 56
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: random number

  #2  
May 12th, 2008
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;
}
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Join Date: Nov 2007
Posts: 73
Reputation: emilio is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster in Training

Re: random number

  #3  
May 13th, 2008
ok.
i wanted to use something simple cause my actual program is quite difficult
i need to do something like this:


  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4.  
  5. int sig,pid;
  6.  
  7. int getRand ( int a , int b )
  8. {
  9. return a+rand()%(b-a+1);
  10. }
  11.  
  12. int main()
  13. {
  14. pid = getpid();
  15. srand ( time( NULL ) ) ;
  16. int i,n;
  17.  
  18. for(i=0 ; i<6 ; i++)
  19. {
  20. if(getpid() == pid)
  21. sig = fork();
  22.  
  23. if(sig == 0)
  24. {
  25. n = getRand(1,9);
  26. printf("%d\n",n);
  27. sleep(n);
  28. exit(0);
  29. }
  30.  
  31.  
  32. return 0;
  33. }

at this point it gives me the same random number.
Reply With Quote  
Join Date: May 2008
Posts: 345
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 56
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: random number

  #4  
May 15th, 2008
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;
}
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Join Date: Nov 2007
Posts: 73
Reputation: emilio is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
emilio emilio is offline Offline
Junior Poster in Training

Re: random number

  #5  
May 15th, 2008
ok it works
thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 2:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC