Thread: Use srand() twice in a function?

  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
    28,413
    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().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    You need to #include <limits.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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