home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / sci / math / symbolic / 3334 < prev    next >
Encoding:
Internet Message Format  |  1992-12-24  |  2.0 KB

  1. Path: sparky!uunet!usc!cs.utexas.edu!wupost!emory!ogicse!news.u.washington.edu!zermelo.math.washington.edu!petry
  2. From: petry@zermelo.math.washington.edu (David Petry)
  3. Newsgroups: sci.math.symbolic
  4. Subject: Re: "shut up and deal the cards"
  5. Message-ID: <1992Dec25.001234.21234@u.washington.edu>
  6. Date: 25 Dec 92 00:12:34 GMT
  7. Article-I.D.: u.1992Dec25.001234.21234
  8. References: <Bzs70r.B1B@news.cso.uiuc.edu>
  9. Sender: news@u.washington.edu (USENET News System)
  10. Organization: University of Washington, Mathematics, Seattle
  11. Lines: 50
  12.  
  13. In article <Bzs70r.B1B@news.cso.uiuc.edu> Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu> writes:
  14. >i figured out a way to deal a bridge hand that's more than three times
  15. >faster than the  version i posted a littel while ago.. 
  16. >
  17. >deck = Flatten[Outer[List,{c,d,h,s},Join[Range[2,10],{J,Q,K,A}]],1];
  18. >
  19. >SeedRandom[2]
  20. >Timing[Partition[Transpose[Sort[Transpose[{Table[Random[],{52}],
  21. >deck}]]][[2]],13];]
  22. >{0.166667 Second, Null}
  23. >
  24. >SeedRandom[2]
  25. > Timing[Partition[Map[#[[2]]&,
  26. >Sort[Array[{Random[],#}&,52]]]/.Thread[Range[52]->deck],13];]
  27. >{0.516667 Second, Null}
  28. >
  29.  
  30.  
  31. In any programming language other than Mathematica, I would write a
  32. function to randomize the elements of a list as follows:
  33.  
  34.  
  35. randomize[L_] := Module[{a,b,k,p=L}, 
  36.             For[k=Length[L],k>1,k--,
  37.                 b=Random[Integer,{1,k}];
  38.                 a=p[[k]];
  39.                 p[[k]]=p[[b]];
  40.                 p[[b]]=a];
  41.             p]
  42.  
  43.  
  44. But, my tests show that my function is about 5 times slower than the
  45. function Mr. Gaylord gives us, and (by actual tests) almost a thousand
  46. times slower than the same program written in C.
  47.  
  48.  
  49. >the speed comes (i think) because i directly create the list
  50. >{...{Random[], card}...} and use Transpose rather than a replacement rule
  51. >(once again reaffirming the efficiency of functional programming.)
  52.  
  53.  
  54. From the clever solutions to this problem that have been posted, I gather
  55. that good Mathematica programmers must learn a bunch of slick tricks to
  56. write reasonably efficient programs.  That bothers me.
  57.  
  58. Does anyone have any comments?
  59.  
  60.  
  61.  
  62. David Petry
  63.