> You suggest:
> x = x * n / sysRandomMax;
As Michael pointed out, that should be (sysRandomMax + 1).
> I suggest:
> x = (double) x / ( (double) (1 + sysRandomMax / n )) ;
Treating this mathematically (ie. ignoring overflow, etc), this is:
x = x / (1 + (sysRandomMax / n))
I assume you meant:
x = x / ((1 + sysRandomMax) / n)
which can be simplified to:
x = x * n / (1 + sysRandomMax)
which is Michael's corrected version.
> Notice that for equation 1 the range is wrong because getting a three
> returned is very unlikely but possible.
Yup - my bad.
> I threw the cast to double in there because I was getting some weird
integer
> math round off error where I always was getting 0 for the equation
No doubt because of the double division. Use the simplified version - the
roundoff issues will be easier to predict.
> I was using unsigned long instead of integer because of carelessness.
You need 32-bit integers to do the math here. A plain "int" wouldn't cut it.
-
Danny
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/