carvalho.miguel@[EMAIL PROTECTED]
twierdzi, że:
> im trying to generate a double precision random number between 0 and
> 1, but i keep getting 0 only.
> static double randomDouble()
> {
> return static_cast<double>(rand() / RAND_MAX);
> }
RAND_MAX is an integer
rand() returns an integer
(rand() / RAND_MAX) is a division of integer by integer, suprisingly
giving the integer :). Further cast doesn't matter, since you can only
get 0 or 1 (if rand() results in RAND_MAX value).
One of them should be a double. My manual says that the proper formula
is:
return rand() / (RAND_MAX + 1.0);
--
If an experiment works, something has gone wrong.


|