Michael Glickman wrote:
> #define RandInt(n)  (((UInt32) SysRandom(0) * (n)) / sysRandomMax)

followed by:
> Sorry, I was wrong, you need +1 and another cast to avoid truncation:
> 
> #define RandInt(n)  (((UInt32) SysRandom(0) * (n)) / ((UInt32)
sysRandomMax
> + 1))

The +1 makes sense. You want to produce numbers from 0 to n-1. If
SysRandom(0) happens to return sysRandomMax, your first version (and my
version) would produce n.

Why another cast? The one cast on the result of SysRandom(0) should cause
the following binary operators to cast automatically. Adding an explicit
cast is probably better style anyway.
-
Danny

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to