drand48 returning 0's and nan

This is a discussion on drand48 returning 0's and nan within the C Programming forums, part of the General Programming Boards category; I'm having trouble using drand48 to feed an array, i tested the array using the loop counter and it stores ...

  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 10:03 PM. Reason: bolded areas pertaining to drand

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    4,415
    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
    22,303
    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 10:30 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

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

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21