> I was wondering if anyone has a better way of "shuffling" records in a data
> file, for
> a quiz application. What I'm doing is taking TimGetTicks as the seed for
> SysRandom,
> using that to randomly pull a record and set an ORDER field. I increment
> the value for
> the ORDER field and do this all over again, hoping to randomly fill my
> records with
> an incrementing number. I then call DmQuickSort sorting on that ORDER
> value.
>
> It's not working too well. Has anybody got a better way to do this? There
> must be one, this stinks.
I normally use the following scheme for shuffling, this scheme is
needs the minmum number of calls to sys random and still yields
each permutation with the same probablility (assuming a decent
random, of course :-)
allocate idx
for( i=0; i<len; i++ )
idx[i] = i;
for( i=len-1; i>0; i-- ) {
j = SysRandom(0) % (i+1);
// note that j might by equal to i,sometimes
swap idx[i] and idx[j];
}
whenever you intend to read record i, read idx[i] instead.
(or scan through db, set order to idx[i] and sort, if you
really want to)
With kind regards / Mit freundlichem Gruß
Holger Klawitter
--
Holger Klawitter
[EMAIL PROTECTED] http://www.klawitter.de
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/