|
-
May 5th, 2008, 07:01 PM
#1
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...
-
May 5th, 2008, 07:33 PM
#2
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.
-
May 6th, 2008, 12:16 AM
#3
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.
-
May 6th, 2008, 01:07 AM
#4
Re: random doubles
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."
-
May 6th, 2008, 03:31 AM
#5
Re: random doubles
I suggest reading these two articles:
Random Numbers Tutorial
Using rand()
-
May 6th, 2008, 07:33 AM
#6
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.
-
May 6th, 2008, 08:26 AM
#7
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....
-
May 6th, 2008, 09:54 AM
#8
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.
-
May 6th, 2008, 09:57 AM
#9
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.
-
May 7th, 2008, 12:42 AM
#10
Re: random doubles
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|