Calling the time function in a C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cdietschrun
    New Member
    • Mar 2008
    • 4

    Calling the time function in a C program

    I have a program that I needed to create for a homework that is an Operating System Simulator. It's not as cool as it sounds. When I run it,

    Code:
      
      long now ;
    
      srand( system( "time(&now)" ) );
    Is supposed to create a value that will initialize the "system clock" ( a static int ).

    The program runs and does what it is supposed to, except there is no randomness in the results ( there should be differences in the amount of "time units", which are measured in blocks of time that is affected by this function call I believe.

    Anyone help me make sure I'm doing this right? It's supposed to put that random # in the variable "now".
  • cdietschrun
    New Member
    • Mar 2008
    • 4

    #2
    I have my entire code if someone wants to read it and tell me why I'm not getting random outputs, that works too.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Originally posted by cdietschrun
      I have a program that I needed to create for a homework that is an Operating System Simulator. It's not as cool as it sounds. When I run it,

      Code:
        
        long now ;
      
        srand( system( "time(&now)" ) );
      Is supposed to create a value that will initialize the "system clock" ( a static int ).

      The program runs and does what it is supposed to, except there is no randomness in the results ( there should be differences in the amount of "time units", which are measured in blocks of time that is affected by this function call I believe.

      Anyone help me make sure I'm doing this right? It's supposed to put that random # in the variable "now".

      are you calling seed() function before calling this?

      Raghuram

      Comment

      • cdietschrun
        New Member
        • Mar 2008
        • 4

        #4
        No, I'm confused by this whole setup. It is from a book and it is 20 years old and they don't explain their code. Our job is to type it in and get it to work in ANSI C and they throw little things like this in there that I can't quite figure out.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by gpraghuram
          are you calling seed() function before calling this?
          srand IS the seed function for the PRNG.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by cdietschrun
            Code:
              
              long now ;
            
              srand( system( "time(&now)" ) );
            This code is not right I think you mean

            Code:
              
              long now ;
            
              srand( time(&now) );
            But you should read up on system and time so that you understand exactly what they do.

            Comment

            Working...