Thread: drand48 returning 0's and nan

  1. #1
    Registered User Destinps's Avatar
    Join Date
    Oct 2012
    Location
    San Antonio, Texas, United States
    Posts
    6

    drand48 returning 0's and nan

    I'm having trouble using drand48 to feed an array, i tested the array using the loop counter and it stores that just fine.
    When I use drand48 though, i get 7 0's in the first 7 spots, and the rest i get nan

    Code:
    /*
    *  Program to write an array to a file
    *  Array will be filled with random numbers using drand48 (0.0, 1.0)
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
       FILE *ofp;
       int i, N;
       double *A;
    
       srand48((long)time(NULL));
       if (argc != 2)
       {
          printf("Error with arguments, must have 2, program name and a integer for array");
          exit(1);
       }
       N = atoi(argv[1]);
    
       A = malloc((N + 1) * sizeof(double));
    
       ofp = fopen("arrout.dat", "w");
    
       fprintf(ofp, "Array size: %d\n", N);
    
       for(i = 0; i < N; ++i)
       {
          A[i] = drand48();
          fprintf(ofp, "%lf\n", A[i]);
       }
    
       return 0;
    }
    Last edited by Destinps; 10-10-2012 at 09:03 PM. Reason: bolded areas pertaining to drand

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that srand48 and drand48 are not part of the C standard library. Check that they really are declared in <stdlib.h>, otherwise you will need to find out what is the correct header to include (EDIT: or macro name to define, or just forward declare them yourself).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Could it be because you're using %lf in fprintf whereas you should be using %f ?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oogabooga
    Could it be because you're using %lf in fprintf whereas you should be using %f ?
    %lf is a valid format specification (at least since C99), though it is redundant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I note that srand48 and drand48 are not part of the C standard library.
    O_o

    I recall that they are POSIX where they will be in "stdlib.h" because the POSIX standard likes to pull double duty from C standard headers for historical reasons.

    Soma

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    %lf is a valid format specification (at least since C99), though it is redundant.
    Yeah, I should have looked that up first.

    As for your point, it turns out that drand48 (and srand48) are declared in stdlib.h.
    I guess it's okay to add things to a standard library header.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User Destinps's Avatar
    Join Date
    Oct 2012
    Location
    San Antonio, Texas, United States
    Posts
    6
    Quote Originally Posted by laserlight View Post
    I note that srand48 and drand48 are not part of the C standard library. Check that they really are declared in <stdlib.h>, otherwise you will need to find out what is the correct header to include (EDIT: or macro name to define, or just forward declare them yourself).
    Code:
    NAME
           drand48,  erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48,
           lcong48 - generate uniformly distributed pseudo-random numbers
    
    SYNOPSIS
           #include <stdlib.h>

    yeah it's in there looks like. I tried seeding a different way and got the same results as well..

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by phantomotap
    I recall that they are POSIX where they will be in "stdlib.h" because the POSIX standard likes to pull double duty from C standard headers for historical reasons.
    Indeed, hence I was surprised to discover that it was not so for me. It is plausible that it may require some macro or something (compiler option?) to "enable" these extensions.

    Quote Originally Posted by Destinps
    yeah it's in there looks like. I tried seeding a different way and got the same results as well..
    If you are showing me a manual page, don't bother: I already know, and that's why I actually tested your program because I couldn't see what was wrong (other than failure to call free(), which shouldn't matter for your problem). Did you compile at the highest warning level? What compiler are you using, on what OS?
    Last edited by laserlight; 10-10-2012 at 09:30 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    Indeed, hence I was surprised to discover that it was not so for me. It is plausible that it may require some macro or something (compiler option?) to "enable" these extensions.
    Compiling with full warning should ferret out that possibility.
    With gcc, use:
    gcc -W -Wall yourprog.c
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User Destinps's Avatar
    Join Date
    Oct 2012
    Location
    San Antonio, Texas, United States
    Posts
    6
    using putty to connect to a school computer from home..
    the school computer is running linux and i'm using gcc to compile
    I'm new to C and Linux so I'm not sure what info is needed, but i compiled using gcc -ansi -g -o

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Destinps View Post
    using putty to connect to a school computer from home..
    the school computer is running linux and i'm using gcc to compile
    I'm new to C and Linux so I'm not sure what info is needed, but i compiled using gcc -ansi -g -o
    Add the -W and -Wall flags to your gcc command.
    Do you get any warnings doing that?

    EDIT: I should mention that your program works fine for me.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User Destinps's Avatar
    Join Date
    Oct 2012
    Location
    San Antonio, Texas, United States
    Posts
    6
    Quote Originally Posted by oogabooga View Post
    Add the -W and -Wall flags to your gcc command.
    Do you get any warnings doing that?

    EDIT: I should mention that your program works fine for me.
    no warnings when compiling with those arguments, but if the program works for you that's good enough for me. I'm just coding it as practice for a test so it's not going to be turned in or anything.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Destinps
    the school computer is running linux and i'm using gcc to compile
    I'm new to C and Linux so I'm not sure what info is needed
    Right. Check your manual page again. On mine, I realised I missed:
    Code:
       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
    
           All functions shown above: _SVID_SOURCE || _XOPEN_SOURCE
    What this means is that if you define the _SVID_SOURCE or _XOPEN_SOURCE macros, then the functions specified will be declared in <stdlib.h>.

    Quote Originally Posted by Destinps
    i compiled using gcc -ansi -g -o
    You should add the flags oogabooga mentioned to increase the warning level. I usually also include -pedantic. So, including the macro that you should define, I would expect:
    Code:
    gcc -ansi -W -Wall -pedantic -D_XOPEN_SOURCE -g -o
    (or -D_SVID_SOURCE, or whatever else you need to define)

    Also, since you are going for -ansi without -std=c99, I think oogabooga's point about %f versus %lf may come into play, but I'm not that familiar with the ins and out of gcc options and the 1989/1990 version of the C standard to say for sure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User Destinps's Avatar
    Join Date
    Oct 2012
    Location
    San Antonio, Texas, United States
    Posts
    6
    D_SVID_SOURCE worked, i read that in the man but had no idea how to comprehend it
    thanks for the help people!
    Last edited by Destinps; 10-10-2012 at 09:50 PM.

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. drand48
    By justgotthis in forum Linux Programming
    Replies: 3
    Last Post: 10-07-2005, 11:48 PM
  5. help with getting drand48() to work
    By v3dant in forum C++ Programming
    Replies: 4
    Last Post: 10-22-2004, 08:43 AM

Tags for this Thread