Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utcsri!rpi!gatech!paladin.american.edu!howland.reston.ans.net!ux1.cso.uiuc.edu!news.cso.uiuc.edu!gaylord.slip.uiuc.edu!gaylord
From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
Subject: Re: Cup of coffee?/the deal function
Date: Mon, 8 Feb 1993 18:55:59 GMT
X-Xxmessage-Id: <A79C08D09E01164B@gaylord.slip.uiuc.edu>
X-Useragent: Nuntius v1.1.1d13
Message-ID: <C258LB.1qw@news.cso.uiuc.edu>
References: <C1yLz0.8ts@watserv1.uwaterloo.ca>
Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
X-Xxdate: Mon, 8 Feb 93 12:55:44 GMT
Organization: University of Illinois
Lines: 86

Subject: Re: Cup of coffee?
From: Robert Bernecky, rbe@yrloc.ipsa.reuter.COM
Date: Mon, 8 Feb 93 15:24:39 GMT
In article <1993Feb8.152439.26726@yrloc.ipsa.reuter.COM> Robert Bernecky,
rbe@yrloc.ipsa.reuter.COM writes:
>
>There are smart and dumb implementations of deal. Some systems actually
>use both, depending on available space. Others are merely dumb.
>A third are merely smart. Doug Forkes and I worked out a mostly
>smart one some time ago. If I can remember what we did, I'll 
>post a model of it.
>
>The dumb one basically picks a random number in the range, then 
>serially examines every number it's generated thus far in the result to
>see if this it has already picked this one. If so, it picks another
>number, searches for it, ...   
>If it's not in the result, it appends it, checks for completion, and
>keeps going if not done.

=======

 i've been writing programs to do 'deal' in Mathematica for my
"Mathematica Progamming Primer" book and for the tutorial "Mathematica
for APL'ers" to be given at the APL 93 conference in August.

first we'll create a deck of cards (not necessary for deal but what the
hell)

Flatten[Outer[List,{c,d,h,s}, Join[Range[2,10],{J,Q,K,A}]],1]
{{c, 2}, {c, 3}, {c, 4}, {c, 5}, {c, 6}, {c, 7}, {c, 8}, {c, 9}, {c, 10},
{c, J}, 
 {c, Q}, {c, K}, {c, A}, {d, 2}, {d, 3}, {d, 4}, {d, 5}, {d, 6}, {d, 7},
{d, 8}, 
 {d, 9}, {d, 10}, {d, J}, {d, Q}, {d, K}, {d, A}, {h, 2}, {h, 3}, {h, 4},
{h, 5}, 
 {h, 6}, {h, 7}, {h, 8}, {h, 9}, {h, 10}, {h, J}, {h, Q}, {h, K}, {h, A},
{s, 2}, 
 {s, 3}, {s, 4}, {s, 5}, {s, 6}, {s, 7}, {s, 8}, {s, 9}, {s, 10}, {s, J},
{s, Q},
 {s, K}, {s, A}}

==================

now, we'll set up a  functional program where we pick an element in a
random location in a list, remove it from the list and then pick another
element from a random location in the remaining list etc.

chooseWithoutReplacement[lis_, n_] :=
      Complement[lis, Nest[(Delete[#, Random[Integer, {1, Length[#]}]
])&, lis, n]] ] 

chooseWithoutReplacement[Range[10], 5]
{1, 3, 7, 8, 9}


this program tells you what was picked but not the order in which they
were picked.

================

here we care about the order of picking from a list (i call this 'lotto'
after the illinois state letter game based on choosing n numbers, each
between 0 and s with no duplicates allowed). 

lotto[lis_, n_] :=  (Flatten[Rest[MapThread[Complement,
   {RotateRight[#], #},1]]])&[ NestList[Delete[#, Random[Integer, {1,
Length[#]}]]&, lis, n]]

chooseHistory1[Range[10], 5]
{8, 3, 9, 7, 1}

===============

here's another way to deal while keeping track of the history.
 
chooseHistory2[lis_, n_] :=
    Take[Transpose[Sort[Transpose[{Table[Random[], {Length[lis]}],
lis}]]] [[2]], n]

this method 'tags' each list element with a random number {Random[], elt}
and then sorts the list (which randomizes the list)  strips off the
Random tag and then takes the first n elements andstrips .

this is pretty interesting since its a really fast way to randomize a
list. and its the fastest way to do deal if ratio of selected elements to
list size is greater than 1/2.
