Thread: drand48

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    drand48

    I am trying to generate random numbers using drand48. I know, there are other random generators out there, but I need to use drand48. The code snippet is as follows:
    Code:
    double seedrandUI()
    {
    	printf("\nin seedaranny\n");
      struct timeval time;
    
      // seed random number generator
      gettimeofday(&time,NULL);
      srand48((unsigned int) time.tv_usec);
      return randInt48InRange(0, 1); // make int in [0,50000] range
    }
    
    double randInt48InRange ( int min, int max )
    {
    	printf("\nIN 48INITI\n");
    	printf("\nMin: %d",min);
    	printf("\nMax: %d",max);
      double r;/* random value in range [0,1) */
      long int M;
      double y;
    
      M = ((long int)(max-min+1));
      printf("\nM is: %ld",M);
    
      r = ( drand48() / (double)(RAND_MAX / M + 1) );
    	printf("\nr is: %ld",r);
    
      y = r+min;
    	printf("\ny as a long is %ld",y);
    
      printf("\nDRAND48 genereated: %ld	\n",y);
    
      return y;
    }
    For some reason, drand48() returns a negative number way above 1 (the range of the random double number needs to be between 0 and 1). What am I doing wrong?

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    You're making it way to complicated. try:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        int i=0, j=0;
        
        char tmp[12]={0x0};
        for(i=0;i<50;i++)
        {
           double v=drand48() * 50001.;
           sprintf(tmp,"%.0f",v);
           j=atoi(tmp);
           printf("%d\n",j);
        }
        return 0;
    }
    output:
    19824
    42025
    17667
    22330
    15935
    44322
    779
    29205
    7969
    19186
    34551
    2943
    44994
    8177
    7954
    26654
    30208
    29136
    13499
    19524
    14670
    37120
    14927
    3777
    20250
    42870
    47099
    33142
    42325
    138
    23119
    26630
    39395
    13281
    49139
    15340
    30043
    30436
    10622
    44296
    15233
    7593
    16883
    19374
    32181
    37678
    30181
    26582
    22968
    32625

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    In my code snippet, I tried to seed the time so I can get random numbers everytime I run the program. I need to generate a random number from 0 to 1, but everytime I try to, I get a negative/positive number that is much much bigger than the range 0 to 1. Is there a way to get a random number from 0 to 1?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("\nDRAND48 genereated: %ld \n",y);
    Try compiling your code with -W -Wall command line options as well.
    You don't use %ld to print a double.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working of drand48()
    By broli86 in forum C Programming
    Replies: 1
    Last Post: 04-11-2008, 11:08 AM
  2. drand48() Probability
    By BENCHMARKMAN in forum C Programming
    Replies: 11
    Last Post: 02-18-2008, 08:57 PM
  3. drand48(): generating pseudo random numbers in a range
    By Isolda_ in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 11:21 AM
  4. help with getting drand48() to work
    By v3dant in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2004, 08:43 AM