From: sposes on
After searching to no evail for an answer to generate double presision
random values I come here for assistance. I am still very new to C++
and have a situation where I need to generate a radom number that
contains a fractional part. I have the following statement:

currentValue = minValue + (rand() % maxValue);

where currentValue, minValue and MaxValue are all of type double. As
it is there is a compile error that states invalid operands of types
'int' and 'double' to binary 'operator%'

I think this means that % requires an int on both sides, but if this is
ture how am i to solve this delema. Im guessing that casting somehow
might help me, but I am not very familiar with this. If I were to cast
maxValue as an int, this might satisfy the % operator problem but
prevents currentValue from ever achiveing a real value such as 3.56

Can someone please direct me on how to aproach this problem.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carlos Moreno on
sposes wrote:
> After searching to no evail for an answer to generate double presision
> random values I come here for assistance. I am still very new to C++
> and have a situation where I need to generate a radom number that
> contains a fractional part. I have the following statement:
>
> currentValue = minValue + (rand() % maxValue);
>
> where currentValue, minValue and MaxValue are all of type double. As
> it is there is a compile error that states invalid operands of types
> 'int' and 'double' to binary 'operator%'
>
> I think this means that % requires an int on both sides, but if this is
> ture how am i to solve this delema. Im guessing that casting somehow
> might help me, but I am not very familiar with this. If I were to cast
> maxValue as an int, this might satisfy the % operator problem but
> prevents currentValue from ever achiveing a real value such as 3.56
>
> Can someone please direct me on how to aproach this problem.

1) If you happen to be working on a Unix/Linux system, they typically
provide a rand48 family of functions which, in addition to being
usually higher quality than the standard rand() function, it does
include a drand48() function that returns a double-precision
floating point between 0 and 1 (including 0, excluding 1).

With this, you can simply use some math to come up with a linear
(or rather, affine) formula to map the range [0,1) to your given
range.

2) If you're restricted to the standard rand() function, perhaps the
second best option would be to map the entire [0,RAND_MAX] range
to your range *of floating point*. This is actually bad, since
you're still restricted to only 4.2 billion possible values, even
if your range contains a higher amount of representable values.

3) Have you checked Boost? (http://www.boost.org) IIRC, they have
several high-quality random numbers generators.


As usual, when asking about random numbers, the necessary question
in response to your question is: what do you need them for?
Depending on the application, the criteria to select "the right
thing to do" can vary quite wildly.

HTH,

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: peter steiner on
sposes wrote:
> After searching to no evail for an answer to generate double presision
> random values I come here for assistance. I am still very new to C++
> and have a situation where I need to generate a radom number that
> contains a fractional part. I have the following statement:
>
> currentValue = minValue + (rand() % maxValue);
>
> where currentValue, minValue and MaxValue are all of type double. As
> it is there is a compile error that states invalid operands of types
> 'int' and 'double' to binary 'operator%'
>
> I think this means that % requires an int on both sides, but if this is
> ture how am i to solve this delema. Im guessing that casting somehow
> might help me, but I am not very familiar with this. If I were to cast
> maxValue as an int, this might satisfy the % operator problem but
> prevents currentValue from ever achiveing a real value such as 3.56
>
> Can someone please direct me on how to aproach this problem.

if you want random floating point values you don't need to use the
modulo operator. rather calculate a value between 0 and 1 with the help
of <cstdlib> RAND_MAX and the use that to get a value inside your
specified range:

double drand(double min, double max)
{
double r = static_cast<double>(rand()) / RAND_MAX; // [0..1]
return min + (max - min) * r;
}

-- peter


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: gerg on
here is a link if you need a fine level of granularity:
http://web.comlab.ox.ac.uk/oucl/work/richard.brent/random.html

in general just do something like this

double rand_val = ((double)rand()) / ((double)MAX_RAND);
double val = (double)minval + rand_va*((double)maxval-(double)minval);

rand() has a real crappy range{0..32767} so using something else is a
good idea.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carlos Moreno on
gerg wrote:

> rand() has a real crappy range{0..32767} so using something else is a
> good idea.

Errr, no. (I mean, yes, rand() is crappy, but for other reasons :-))

RAND_MAX is implementation-dependent -- on my platform, it is
2^31 - 1 (approx. 2.1 billion)

Carlos
--

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]