Thread: Proper use of srand()

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Proper use of srand()

    The exercise was to generate 1000 random numbers between 0-9 and to print how many of each number were generated - and to repeat the task with ten different seeds. I used srand() with time() and yet I get the same distribution of numbers each time. Why is this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
       int nums[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int i,j;
        printf("1     2     3     4     5     6     7     8     9     10\n");
      
        for (i = 0; i < 10; i++)
        {
            srand((unsigned int) time(0));
            for (j = 0; j < 1000; j++)
                nums[(rand() % 10)]++;
    
            for (j = 0; j < 10; j++)
            {
                printf("%-6d", nums[j]);
                nums[j] = 0;
            }
            printf("\n");
        }
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    time() only has second precision. Chances are your program finishes well within 1 second, so all calls to srand have the same seed.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Only probable explanation is that the random number seed isn't changing and that's why you are getting a uniform distribution.
    Perhaps sleep() for a while and see if that changes it to a random distribution. Just my 2c.

  4. #4

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If the exercise simply calls for the use of ten seeds, you can do something simple like:
    Code:
    for(int i = 0; i < 10; i++)
    {
      srand(i);
      /* ... */
    }
    It sounds like the point is to see how the PRNG works, so it would hardly matter which seeds are used.

    My C library treats srand(0) and srand(1) identically; perhaps it would make sense to use srand(i + 1).

  6. #6
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Aha! Using i for the argument to srand() does the trick. I didn't know time() was only to the second! I thought it was like the timer on my old CBM-64....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. srand() no working as expected
    By guesst in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 12:24 PM
  2. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 05:01 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand()... possible reasons for failure
    By lightatdawn in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 02:33 AM