CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: random doubles

  1. #1
    Join Date
    Jul 2007
    Posts
    273

    random doubles

    Hello,
    I read this to generate double random value; but it assumes we are using .NET ( RAND_MAX, I mean). I'd like write a code that can compile on other platform; what can I do? Furthermore: I need a range -0.5 and 0.5; so I set Min = -0.5 and Max 0.5; I hope the negative Min won't get surprises...

  2. #2
    Join Date
    Feb 2003
    Posts
    377

    Re: random doubles

    rand() and RAND_MAX are standard in C++. They should work on other platforms, it's just that RAND_MAX might have a different value.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: random doubles

    Should've read the question.

    I've done that now and I cannot help suspecting that the formula given in that link is wrong. It goes like this,

    int I = IMin + rand() % (IMax - IMin);

    Say IMin=0 and IMax=10. This means you want a random int in the range from 0 up to and including 10, doesn't it? If you enter those numbers into the formula you get,

    int I = 0 + rand() % (10-0);

    Whatever number rand() generates the modulo 10 operation will transform it to a number between 0 and 9 so the resulting int will be a number in the range from 0 up to and including 9 and this wasn't what you expected.

    The correct formula is

    int I = IMin + rand() % (IMax - IMin + 1);

    It will generate a random int in the range from IMin up to and including IMax.

    ---

    The second formula is problematic too,

    double X = XMin + rand() * (XMax - XMin) / RAND_MAX;

    It generates a random double between XMin and up to and including XMax. This is not standard. Normally the upper limit is not inclusive.

    ---

    So the first formula doesn't include the upper limit which it should, and the second formula does include the upper limit which it shouldn't.

    While the first formula is downright wrong the second can be used if you accept that the upper limit is inclusive. But make sure the variables are floating point. If they're integers the formula may fail.

    A better formula is,

    double r = double(rand())/(double(RAND_MAX)+1.0) // random double in range 0.0 to 1.0 (non inclusive)
    double X = XMin + r*(XMax - XMin); // transform to wanted range
    Last edited by _uj; May 6th, 2008 at 02:30 AM.

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: random doubles

    Look up the Mersenne Twister.

    For example
    http://www-personal.umich.edu/~wagne...neTwister.html
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: random doubles

    I suggest reading these two articles:
    Random Numbers Tutorial
    Using rand()
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: random doubles

    RAND_MAX is not .NET-specific. It is part of the standard library so there is no reason it should stop you from using that code as platform-independent.

    Just a quick look indicates that negative numbers will work - all you need to do is try it.

    Also note that the function you refer to is not exactly a random double generator. It is a generator with a specific frequency distribution. The actual random double part is quite simple:

    Code:
    double X = XMin + rand() * (XMax - XMin) / RAND_MAX;
    The only problem with that code is that it lacks precision - i.e. it is limited to RAND_MAX over the range that you are interested in. However, it should be perfectly sufficient for a -0.5 to +0.5 range as long as your demands are not too great.

  7. #7
    Join Date
    Jul 2007
    Posts
    273

    Re: random doubles

    Sincerely, I need my randon value is -0.5 <= value <= + 0.5; It's better the bounds are included to my aim....

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: random doubles

    Apparently on my system RAND_MAX is limited by the value of a signed short. If I need a random value over a larger range----say, 0 to 127999----what's the best approach to use?

    At the moment I'm using (rand()<<15 | rand()) % 128000, but I'm told that two sequential random numbers are not completely independent.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: random doubles

    Apparently on my system RAND_MAX is limited by the value of a signed short. If I need a random value over a larger range----say, 0 to 127999----what's the best approach to use?
    I think the simplest approach is just to use the Mersenne Twister, e.g., the implementation from Boost, or the one from the website I linked to in post #5.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: random doubles

    Quote Originally Posted by laserlight
    I think the simplest approach is just to use the Mersenne Twister, e.g., the implementation from Boost, or the one from the website I linked to in post #5.
    Yes, and random generators are becoming part of the next C++ standard. They're specified in the so called TR1 which is already available with some compilers including VS2008.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured