Thread: drand48() Probability

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    drand48() Probability

    I am trying to use drand48() with a .6 probability of getting a 1. How can I do this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you want a distorted distribution, where you have 60% chance of 1.0, and 40% chance of 0 to "just under 1"? How do you want the second part of the distribution to be formed?

    If you want the 40% portion to be linear, perhaps this would work:
    Code:
    double myrand()
    {
       double x;
        
       x = drand48();
       if (x >= 0.6) x = 1;
       else x = x / 0.6;   /* Extend the range to 0..0.999999 */
    }
    --
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by BENCHMARKMAN View Post
    I am trying to use drand48() with a .6 probability of getting a 1. How can I do this?
    That's a bit like asking for a 5-letter word containing an 'E'.
    What kind of distribution are you after, and what are the minimum and maximum values of the resulting random numbers?

    Are you just wanting a one or a zero? Something like this perhaps?:
    return drand48() >= 0.4 ? 1 : 0;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    i am sorry if this is off topic, but i am curious.
    Is this "drand48()" function ANSI C89?
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't think so.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    I am looking to have 60% chance of getting a 1 and 40% chance of getting a 0.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this "drand48()" function ANSI C89?
    It is non-standard with respect to C.

    I am looking to have 60% chance of getting a 1 and 40% chance of getting a 0.
    I think you can use iMalc's suggestion.
    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

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Thanks for the replies. I am also supposed to use srand48() to nitialize The random number stream with a seed of 9. I'm a little confused here. Somebody explain this?

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I am not sure, but ... srand48(9) might be the answer.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So call
    Code:
     srand48(9L);
    That will guarantee that the random numbers are always the same every time.

    --
    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
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    but how do I use srand()48 with drand48(). I'm confused.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BENCHMARKMAN View Post
    but how do I use srand()48 with drand48(). I'm confused.
    Just like srand() with rand() in standard C, srand48() just sets the "seed" value for the random number generator. Fixing the seed value means that the random number generator gives the same values every time you run the program. So, as mentioned above in multiple places, pass the seed as a parameter with srand48() once at the very beginning of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working of drand48()
    By broli86 in forum C Programming
    Replies: 1
    Last Post: 04-11-2008, 11:08 AM
  2. drand48(): generating pseudo random numbers in a range
    By Isolda_ in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 11:21 AM
  3. drand48
    By justgotthis in forum Linux Programming
    Replies: 3
    Last Post: 10-07-2005, 11:48 PM
  4. keeping stats - probability
    By -JM in forum C++ Programming
    Replies: 6
    Last Post: 08-20-2005, 09:52 AM
  5. 0% probability == impossible?
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 09-01-2003, 03:48 PM