home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!cs.utexas.edu!wupost!emory!ogicse!news.u.washington.edu!zermelo.math.washington.edu!petry
- From: petry@zermelo.math.washington.edu (David Petry)
- Newsgroups: sci.math.symbolic
- Subject: Re: "shut up and deal the cards"
- Message-ID: <1992Dec25.001234.21234@u.washington.edu>
- Date: 25 Dec 92 00:12:34 GMT
- Article-I.D.: u.1992Dec25.001234.21234
- References: <Bzs70r.B1B@news.cso.uiuc.edu>
- Sender: news@u.washington.edu (USENET News System)
- Organization: University of Washington, Mathematics, Seattle
- Lines: 50
-
- In article <Bzs70r.B1B@news.cso.uiuc.edu> Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu> writes:
- >i figured out a way to deal a bridge hand that's more than three times
- >faster than the version i posted a littel while ago..
- >
- >deck = Flatten[Outer[List,{c,d,h,s},Join[Range[2,10],{J,Q,K,A}]],1];
- >
- >SeedRandom[2]
- >Timing[Partition[Transpose[Sort[Transpose[{Table[Random[],{52}],
- >deck}]]][[2]],13];]
- >{0.166667 Second, Null}
- >
- >SeedRandom[2]
- > Timing[Partition[Map[#[[2]]&,
- >Sort[Array[{Random[],#}&,52]]]/.Thread[Range[52]->deck],13];]
- >{0.516667 Second, Null}
- >
-
-
- In any programming language other than Mathematica, I would write a
- function to randomize the elements of a list as follows:
-
-
- randomize[L_] := Module[{a,b,k,p=L},
- For[k=Length[L],k>1,k--,
- b=Random[Integer,{1,k}];
- a=p[[k]];
- p[[k]]=p[[b]];
- p[[b]]=a];
- p]
-
-
- But, my tests show that my function is about 5 times slower than the
- function Mr. Gaylord gives us, and (by actual tests) almost a thousand
- times slower than the same program written in C.
-
-
- >the speed comes (i think) because i directly create the list
- >{...{Random[], card}...} and use Transpose rather than a replacement rule
- >(once again reaffirming the efficiency of functional programming.)
-
-
- From the clever solutions to this problem that have been posted, I gather
- that good Mathematica programmers must learn a bunch of slick tricks to
- write reasonably efficient programs. That bothers me.
-
- Does anyone have any comments?
-
-
-
- David Petry
-