Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: srand(time(NULL)) error message

  1. #1
    Join Date
    Dec 2011
    Beans
    63

    srand(time(NULL)) error message

    Hi
    I'm not a programmer, but I want to get some C code to work for some study that I'm doing in cognitive psychology. It's a mathematical model; it does a calculation and returns a value. The code was written in 1996, and when I try to compile it (I'm typing gcc filename), I get a number of error messages. The main one relates to the random number generator.
    The original code was:
    Code:
     #include "ran1.c"
    /* random seed */
    long     IDUM    = -3;
    I have replaced only the first line with:

    Code:
    #include <time.h>
    
    srand(time(NULL));
    int r = rand();
    but I get this message:
    Code:
    43:7: error: expected declaration specifiers or ‘...’ before ‘time’
    
    44:1: error: initialiser element is not constant
    I think I'm heading in the right direction, but I don't know enough about programming to get it right. I really don't want it to return the wrong value.
    Any help would be appreciated. Thank you.

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: srand(time(NULL)) error message

    It would be immensely helpful to see the actual implementation you have versus a snip of code as you have provided.

    A simple example of using srand() and rand() would be something like:
    Code:
    #include <stdlib.h>
    #include <time.h>
    
    const unsigned int MAX_NUM = 1000;
    
    int main()
    {
        srand(time(NULL));
    
        unsigned int num = rand() % MAX_NUM;   // num can range from 0 through (MAX_NUM - 1)
    }

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: srand(time(NULL)) error message

    Just a note of caution about PRNGs - based on the name (ran1.c) and the negative-long seed (IDUM) it looks like you are trying to replace the Numerical Recipes implementation? If so, that's a long-period floating point PRNG - depending what your model is doing (and how many times it's doing it) the system rand() may or may not be a suitable replacement.

  4. #4
    Join Date
    Dec 2011
    Beans
    63

    Re: srand(time(NULL)) error message

    Thanks for your helpful comments. Any information I can gather is helpful.
    Quote Originally Posted by dwhitney67 View Post
    It would be immensely helpful to see the actual implementation you have versus a snip of code as you have provided.
    Does this make it clearer?
    model.txt
    The attached model.txt file is the C code that I think might be relevant. I've just killed about 2/3 of the lines from the middle of the file.

    Just to recap, when I tried to compile this, I got an error message in relation to the random generator.
    Thank you.
    Last edited by lisati; March 9th, 2013 at 04:34 AM. Reason: Change HTML tags to QUOTE

  5. #5
    Join Date
    Apr 2012
    Beans
    7,256

    Re: srand(time(NULL)) error message

    Is there any particular reason you need to replace the original Numerical Recipes ran1 implementation? The license terms are not onerous afaik

    http://apps.nrbook.com/c/index.html

    I'd suggest including the necessary declarations and compiling the ran1.c file separately rather than doing #include "ran1.c" though

  6. #6
    Join Date
    Dec 2011
    Beans
    63

    Re: srand(time(NULL)) error message

    ok, Thanks again.
    Code:
    rip@Duck:/media/win/arjuna/current_study$ gcc memnew.c
    memnew.c:22:18: fatal error: ran1.c: No such file or directory
    compilation terminated.
    Maybe I need to install something. This is Xubuntu Precise.

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: srand(time(NULL)) error message

    If it's the routine I think it is, you would need to get it from Numerical Recipes

    If you buy the book, you can just type it in (it's only ~ 50 lines of code) - or you can get an electronic subscription

    http://www.nr.com/aboutNR3electronic.html

  8. #8
    Join Date
    Dec 2011
    Beans
    63

    Re: srand(time(NULL)) error message

    Thank you.

  9. #9
    Join Date
    Dec 2011
    Beans
    63

    ran1.c includes base.h

    The code I have ran1.c includes:
    Code:
    #include "base.h"
    Where do I look for that?
    Am I on the right track?

    I'd suggest including the necessary declarations and compiling the ran1.c file separately rather than doing #include "ran1.c" though
    I'm not sure what the necessary declarations are. I'm very low level here. As I say, I'm interested in a mathematical model in cognitive psychology, I'm not a programmer.
    Thanks again.

  10. #10
    Join Date
    Dec 2011
    Beans
    63

    error in fprintf: should be struct

    Hello
    I'm asking for help with format in C. The code has this line:
    Code:
     fprintf( OUTD , "%s" , SPARAM[ i ] );
    And the error is:
    HTML Code:
    format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘struct PARAM’ [-Wformat]
    And I think you might need to know this:
    Code:
    /* strings used by the program */
    #define  NPARAM 12
    struct PARAM
    {
      char *word;
    } SPARAM[ NPARAM ] =
    {
      "Length          ",
      "Strength        ",
      "Frequency       ",
      "Criterion Used  ",
      "Criterion ROC   ",
    I've cut it short here, but I think that's enough.

    I can see what the problem is, but my programming ability is very low level, and I have no idea about C. I need to change the "%s", right?
    I need to get this old C code working. Fortunately, I've had some good help here, and this is the only error message left.
    Comments gratefully received.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •