Re: Progamming Challenge


[Prev][Next][Index][Thread]

Re: Progamming Challenge



Yesterday I posted the following code for "shuffling" a list:
>>  seq(X,X,1,60,1) -> L1
>>  For(A,60,2,-1)
>>   Int(A*rand)+1 -> B
>>   If B<A :Then ****
>>    L1(A) -> X
>>    L1(B) -> L1(A)
>>    X -> L1(B)
>>   End  ****
>>  End

I realized afterward that the two lines marked with "****" above are not
necessary.  Also, thanks to RWW Taylor for giving a reference to Knuth for
this algorithm.

Later yesterday, Runor49 contributed this:
>I made a card game and I ran into a similar
>problem. So I made a list and accessed the elements with the following code
>(note: initialize y as 51 before you want to run) Oh, this is for an 83 also.
>       51->y
>      ============
>      LBL A
>      randInt (0,y)->X
>      L1(X)->A
>      0->L1(X)
>      sortd(L1)     <- this command sorts the list from high to low
>      y-1->y
>      GOTO A

This reminded me of another method for list-shuffling:
  seq(X,X,1,60,1) -> L1
  seq(rand,X,1,60,1) -> L2
  SortA(L2,L1)
This has the advantage of being nice and short, and since it uses the
built-in sort routine, it works faster than a For(...) loop.

 /      Darryl K. Nester        |  nesterd@bluffton.edu  \
/  Assoc. Prof. of Mathematics  |  Office: 419-358-3483   \
\       Bluffton College        |  Fax:    419-358-3232   /
 \  Bluffton, OH  45817-1704    |  Home:   419-358-9650  /


References: