Thread: random?

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    random?

    I completely confuse using rand(). Its generating same number, even using srand()...

    Is there something like Math.random() in C?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    rand() generates a seemingly non-related sequence of numbers initialized by srand()

    So for the same srand() you are probably getting the same sequence of numbers. Or at least the same first number.
    Use something like srand(time(NULL), or something similar so also the initialization depends on something random and changing.
    Last edited by C_ntua; 06-20-2008 at 02:27 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    I completely confuse using rand(). Its generating same number, even using srand()...

    Is there something like Math.random() in C?
    How are you using srand() - rand() gives the same series when given the same seed (srand's argument is the seed for the random number).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read Prelude's articles on:
    Using rand()
    Random Numbers
    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

  5. #5
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    you have to initialize it the proper way:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        srand(time(NULL));
        int i;
        for(i = 0; i < 10; i++)
            printf("%d\n", rand());
        return(0);
    }
    should generate 10 different numbers. Well the chance of having rand() return the same value ten times in a row is extremely low

    Also, you are already discussing a similar problem in another thread

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    If you seed it with the same value, it will always return the same sequence.

    If you dont seed it , it will always return the same sequence as the unseeded sequence.

    If you seed it with a random seed, the sequence repeats after a very short period.

    rand() is only pseudo-random.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    srand(time(0)) will not work if we execute the application twice (fast)...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    srand(time(0)) will not work if we execute the application twice (fast)...
    Indeed. Have you read the articles that I linked to?
    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

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by audinue View Post
    srand(time(0)) will not work if we execute the application twice (fast)...
    Did you read what I said?

    If you seed with the same value you will get the same sequence. If you execute realyl fast, time(0) wil return the same value, thus you will use the same seed value, hence you will get the same sequence.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    srand(time(0)) will not work if we execute the application twice (fast)...
    Correct, since time(0) returns an value which is precise down to seconds, and if you run it twice in a row quickly, you get the same second (and even if it's only one or two more, it may not produce significantly different values).

    If you really require that mode of operation (random numbers created each time the application runs and the application can be started several times the same second), then you need to use some other method of seeding. Some sort of high-precision time would be a good choice - but how you get that would be OS and/or compiler/runtime dependant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    laserlight:
    I've tried their codes that provides the same results.

    Hohoho... ^_^
    I've got the simplest way!
    Try: srand(GetCurrentProcessId())

    Unfortunately its only in Windows...

    EDIT:
    Use _getpid() to replace GetCurrentProcessId().

    Is it safe to do something extreme like: srand(_getpid() * time(0))?
    Last edited by audinue; 06-20-2008 at 02:59 PM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by audinue View Post
    laserlight:
    I've tried their codes that provides the same results.

    Hohoho... ^_^
    I've got the simplest way!
    Try: srand(GetCurrentProcessId())

    Unfortunately its only in Windows...
    Yes, and current processid isn't particularly "random" either.

    Better to get the low word of the TSC or QueryPerformanceCounter() for example. TSC will walk all the way through 32 bits within about 2 seconds on a modern machine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I've tried their codes that provides the same results.
    The point that I am getting at is to understand, not simply try out code hand outs.

    One thing to ask is this: why is your program being executed multiple times in quick succession? If this is a normal mode of operation, then perhaps you should maintain state with a database.
    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

  14. #14
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    No, I just want to make a real random number generator...

    And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I just want to make a real random number generator...
    Consider a setup such as the one used for HotBits or the website that shawnt's Quantum Random Bit Generator gets its randomness from.

    And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...
    That shows that you are ignorant because you have failed to read the articles pointed out to you. The accuracy (or more accurately: precision) does not matter, unless you are using the generator wrongly. The idea behind a pseudo-random number generator is to seed once (and perhaps again after the sequence is exhausted), then use the sequence generated. It is not to keep on seeding and then using the first number in each sequence.

    EDIT:
    If you are talking about the predictability of time(NULL), or that srand(time(NULL)) is not guaranteed to be portable as Prelude pointed out, or that the standard random number generator facility is often not of good quality, then that is another matter. Admittedly, the predictability of time(NULL) is related to its precision in seconds, but the point is that it is rand() that generates the random numbers, with srand() there to "select" a sequence to generate.
    Last edited by laserlight; 06-21-2008 at 02:46 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM