TIP: Click on subject to list as thread! ANSI
echo: quik_bas
to: KURT KUZBA
from: DAVID WILLIAMS
date: 1998-04-20 16:40:00
subject: using RND

-> You know... I think I've seen the like before! :)
Probably. It's hard to come up with any idea that is completely new!
-> '_|_|_|   Where INTEGER types are used, the SWAP command may be used
-> '_|_|_|   in place of the clumsy string CHR$() method used here.
Actually, SWAP works even better on string variables than on numerical
types. With strings, it just fiddles with the pointers, so the actual
text is not rewritten at all. Not only is this fast, it also creates no
"garbage", so string compactions are not caused. However, SWAP can be
used only on complete strings, e.g. SWAP A$, B$. It can't be used on
chunks of the middles of strings, defined by MID$. Sometimes, this is a
good argument for setting things up so many short strings are involved,
that can be SWAPped, rather than using a few longer ones and changing
them with MID$.
So, in place of:
-> FUNCTION shuffle$
-> d$ = "": FOR t% = 1 TO 52: d$ = d$ + CHR$(t% - 1): NEXT
-> FOR t% = 1 TO 500
->     s% = (RND * 999) MOD 52 + 1: c% = t% MOD 52 + 1
->     s$ = MID$(d$, c%, 1)
->     MID$(d$, c%, 1) = MID$(d$, s%, 1)
->     MID$(d$, s%, 1) = s$
-> NEXT: shuffle$ = d$
-> END FUNCTION
it might be better to have an array, in place of the long string d$. So
the routine would become something like:
FUNCTION shuffle$
  FOR t% = 1 to 52: d$(t%) = CHR$(t% - 1): NEXT
  FOR t% = 1 TO 500
     SWAP d$((RND * 999) MOD 52 +1), d$(t% MOD 52 +1)
  NEXT
  shuffle$ = "": FOR t% = 1 to 52: shuffle$ = shuffle$ + d$(t%): NEXT
END FUNCTION
Of course the d$() array would have to have been DIMmed back at the
beginning of the program.
This would maybe speed things up a tad. However, the shuffling method is
still *very* inefficient. It would be *much* better to put the following
code into the program initialization section:
DIM d$(52)
FOR t% = 1 TO 52: d$(t%) = CHR%(t%): NEXT
Then the shuffle function would just become:
FUNCTION shuffle
   shuffle$ = ""
   FOR t% = 1 TO 51
     s% = t% + INT((53 - t%) * RND)
     IF s% > t% THEN SWAP d%(s%), d$(t%)
     shuffle$ = shuffle$ + d$(t%)
   NEXT
   shuffle$ = shuffle$ + d$(52)
END FUNCTION
Much better!
                           dow
--- PCBoard (R) v15.3 (OS/2) 5
---------------
* Origin: FidoNet: CAP/CANADA Support BBS : 416 287-0234 (1:250/710)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.