Re: Programming Challenge
[Prev][Next][Index][Thread]
Re: Programming Challenge
> At 2:50 PM -0100 4/22/98, Steve Muckle wrote:
> >I am trying to make a program that will simulate a deck of cards for
> a
> >collectable card game. I would rather lay out a set of numbers at the
> >beginning (each number representing a particular card) than pick them
> on
> >the fly. What my problem is is that I need to get a list (or a matrix,
> >it doesn't matter) of numbers 1 through 60, randomly assorted, BUT no
> >number appearing more than once.
>
> The easiest way to do this is to first make an ordered list from 1-60,
> then move the elements around randomly. Here's the idea (I haven't
> checked this
> on my calculator, but I've done this sort of thing before; this should
> pretty close to correct). Spaces added for ease of reading.
>
> 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
>
>
> / 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 /
This is, in fact, the most effective and efficient algorithm to produce
a randomly-arranged list from an initially-given one. Looks strange at
first, but in fact the number 1 has an equal chance of winding up in any
given position. Same with the number 2, etc. It (or a variation) is
given in Knuth as the canonical algorithm for this purpose. Depending
on the language environment you are working in it may be possible to
make the swapping part of the algorithm more efficient (this is where
the slows come in). But if you are only shuffling the deck occasioanlly
it shouldn't matter much.
RWW Taylor
National Technical Institute for the Deaf
Rochester Institute of Technology
Rochester NY 14623
>>>> The plural of mongoose begins with p. <<<<