I have been using this function to generate a "random" number:

static UInt16 rand( UInt16 max )
{
 return ( SysRandom(TimGetTicks()) % max );
}

Unfortunately, this only works as expected on an emulated Palm IIIc and
fails on all other devices.  On the devices on which it fails, the problem
is that successive calls in a tight loop generate the same number.  For
example:

for ( i=0; i<25; i++ )
    a[i] = rand(5) + 1;

On an emulated Palm Vx, this might produce the sequence
4,5,5,1,1,2,4,4,5,5,1,3,3,4,4,5,2,2,3,3,4,1,1,2,2

On some devices it is even worse, producing sequences like
4,4,4,4,1,1,3,3,5,5,5,5,2,2,2,2,2,2,3,3,4,4,4,4,2

On an emulated Palm IIIc, this produces a more reasonable
3,4,5,2,3,4,1,2,3,4,2,3,5,1,2,4,5,1,4,5,1,3,4,5,2

Does anyone know why this is happening?  Does anyone have a better idea for
generating a more random series?



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