Hi to all

I put cast in case n is Int16.
Given that SysRandom(0) is also Int16, I assumed that
the result type is Int16 that means truncation.

This assumption might be wrong: I need to get back to
K&R and have a look :=)

Michael



-----Original Message-----
From: Danny Epstein [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 22 November 2001 9:22
To: Palm Developer Forum
Subject: RE: get a random number within a specified range ( a
correction) _


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/

-- 
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