Use srand() twice in a function?

This is a discussion on Use srand() twice in a function? within the C Programming forums, part of the General Programming Boards category; Can I use the srand() function twice in a function like so? Code: void delaycalc(void) { int one; int result; ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Use srand() twice in a function?

    Can I use the srand() function twice in a function like so?

    Code:
    void delaycalc(void)
    {
        int one;
        int result;
        
        srand((unsigned)time(NULL));
        one = rand();
        
        srand(one);
        result = rand() % 1200;
        
        printf("%i", result);
    }
    You see I'm trying to get random numbers that are 1200 or less. However, when I use the time function to make a seed and loop the function so it displays 20 random numbers, it increments slowly like 2390, 2394, 2398 etc.

    So it's hardly random. So can I use two srand() functions to try and randomise it more (Preferably so the values move down sometimes aswell)?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by Jake.c
    You see I'm trying to get random numbers that are 1200 or less. However, when I use the time function to make a seed and loop the function so it displays 20 random numbers, it increments slowly like 2390, 2394, 2398 etc.
    You could... wait longer before running the program again

    Prelude suggests that you hash the value returned by time(). For an example, read her article on using rand().
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you should not use srand twice - it will descrease the randomness of your results, not increase them

    Check the http://www.eternallyconfuzzled.com/a..._art_rand.aspx about more info
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Now I have the problem of the function that the eternally confused tutorial gives at the end. When I try to compile it says:

    Code:
    unsigned readySeed()
    {
             time_t now = time ();
             unsigned char *p = (unsigned char *)&now;
             unsigned seed = 0;
             size_t i;
     
             for ( i = 0; i < sizeof now; i++ )
                 seed = seed * ( UCHAR_MAX + 2U ) + p[i];
     
     return seed;
    }
    `UCHAR_MAX' undeclared (first use in this function)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    You need to #include <limits.h>
    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

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Oh thankyou very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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