Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to call srand(time(NULL));

how to call srand(time(NULL));

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrby123
    wrote on last edited by
    #1

    how to call srand(time(NULL)); When compile, I got: : error C2064: term does not evaluate to a function at the line: srand(time(NULL)); Please help to identify what I missing?

    X 1 Reply Last reply
    0
    • M mrby123

      how to call srand(time(NULL)); When compile, I got: : error C2064: term does not evaluate to a function at the line: srand(time(NULL)); Please help to identify what I missing?

      X Offline
      X Offline
      Xing Chen
      wrote on last edited by
      #2

      try this:

      #include <stdlib.h>
      #include <stdio.h>
      #include <time.h>

      int main( void )
      {
      int i;

      // Seed the random-number generator with current time so that
      // the numbers will be different every time we run.
      //
      srand( (unsigned)time( NULL ) );

      // Display 10 numbers.
      for( i = 0; i < 10;i++ )
      printf( " %6d\n", rand() );

      printf("\n");

      // Usually, you will want to generate a number in a specific range,
      // such as 0 to 100, like this:
      {
      int RANGE_MIN = 0;
      int RANGE_MAX = 100;
      for (i = 0; i < 10; i++ )
      {
      int rand100 = (((double) rand() /
      (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
      printf( " %6d\n", rand100);
      }
      }
      }

      P D M 3 Replies Last reply
      0
      • X Xing Chen

        try this:

        #include <stdlib.h>
        #include <stdio.h>
        #include <time.h>

        int main( void )
        {
        int i;

        // Seed the random-number generator with current time so that
        // the numbers will be different every time we run.
        //
        srand( (unsigned)time( NULL ) );

        // Display 10 numbers.
        for( i = 0; i < 10;i++ )
        printf( " %6d\n", rand() );

        printf("\n");

        // Usually, you will want to generate a number in a specific range,
        // such as 0 to 100, like this:
        {
        int RANGE_MIN = 0;
        int RANGE_MAX = 100;
        for (i = 0; i < 10; i++ )
        {
        int rand100 = (((double) rand() /
        (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
        printf( " %6d\n", rand100);
        }
        }
        }

        P Offline
        P Offline
        Peter_in_2780
        wrote on last edited by
        #3

        Xing Chen wrote:

        int rand100 = (((double) rand() /
        (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);

        ftfy:

        int rand100 = (((double) rand() /
        (double) RAND_MAX) * (RANGE_MAX - RANGE_MIN) + RANGE_MIN);

        Software rusts. Simon Stephenson, ca 1994.

        modified on Thursday, May 6, 2010 3:17 AM

        1 Reply Last reply
        0
        • X Xing Chen

          try this:

          #include <stdlib.h>
          #include <stdio.h>
          #include <time.h>

          int main( void )
          {
          int i;

          // Seed the random-number generator with current time so that
          // the numbers will be different every time we run.
          //
          srand( (unsigned)time( NULL ) );

          // Display 10 numbers.
          for( i = 0; i < 10;i++ )
          printf( " %6d\n", rand() );

          printf("\n");

          // Usually, you will want to generate a number in a specific range,
          // such as 0 to 100, like this:
          {
          int RANGE_MIN = 0;
          int RANGE_MAX = 100;
          for (i = 0; i < 10; i++ )
          {
          int rand100 = (((double) rand() /
          (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
          printf( " %6d\n", rand100);
          }
          }
          }

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          What exactly does casting the return value of time() to unsigned do to address the C2064 error?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          1 Reply Last reply
          0
          • X Xing Chen

            try this:

            #include <stdlib.h>
            #include <stdio.h>
            #include <time.h>

            int main( void )
            {
            int i;

            // Seed the random-number generator with current time so that
            // the numbers will be different every time we run.
            //
            srand( (unsigned)time( NULL ) );

            // Display 10 numbers.
            for( i = 0; i < 10;i++ )
            printf( " %6d\n", rand() );

            printf("\n");

            // Usually, you will want to generate a number in a specific range,
            // such as 0 to 100, like this:
            {
            int RANGE_MIN = 0;
            int RANGE_MAX = 100;
            for (i = 0; i < 10; i++ )
            {
            int rand100 = (((double) rand() /
            (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
            printf( " %6d\n", rand100);
            }
            }
            }

            M Offline
            M Offline
            mrby123
            wrote on last edited by
            #5

            I put you suggested. I got the same problem in my program. I tested in a small program, it OK. Someconflict with my program. Thanks

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups