home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / sci / math / symbolic / 3332 < prev    next >
Encoding:
Text File  |  1992-12-24  |  3.2 KB  |  95 lines

  1. Newsgroups: sci.math.symbolic
  2. Path: sparky!uunet!wupost!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!gaylord.slip.uiuc.edu!gaylord
  3. From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
  4. Subject: Re: how to randomize the elements in a list?
  5. References: <BzrsII.7vu@news.cso.uiuc.edu>
  6. Message-ID: <Bzs4MK.AJn@news.cso.uiuc.edu>
  7. X-Xxdate: Thu, 24 Dec 92 13:55:13 GMT
  8. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  9. X-Useragent: Nuntius v1.1.1d13
  10. Organization: University of Illinois
  11. Date: Thu, 24 Dec 1992 19:54:19 GMT
  12. X-Xxmessage-Id: <A75F71C19201164B@gaylord.slip.uiuc.edu>
  13. Lines: 80
  14.  
  15. gaylord@ux1.cso.uiuc.edu (Richard J. Gaylord)
  16.  
  17. thanks to all the santas out there.
  18.  
  19. Hal Varian pointed out that the standard package
  20. DiscreteMath/Permutations.m contains the following definition:
  21.  
  22. RandomPermutation[n_Integer?Positive] :=
  23.         Block[{t},
  24.                 t = Array[{Random[], #} &, n];
  25.                 t = Sort[t];
  26.                 Map[ #[[2]] &, t ]
  27.         ]
  28.  
  29. this is really clever [those wri guys ain't too shoddy] since it uses the
  30. built-in Random function which is quite fast.
  31.  
  32. functionalizing this (so as not to offent my aethetic sensibilities), we
  33. can write
  34.  
  35. randomPermutations[n_] := Map[#[[2]]&,Sort[Array[{Random[],#}&, n]]]
  36.  
  37. an alternative to using Array is, of course, to use Table
  38.  
  39. Map[#[[2]]&,Sort[Table[{Random[],i},{i,n}]]]
  40.  
  41.  
  42. there seems to be a small (miniscule) speed advantage to Table over Array
  43.  
  44. SeedRandom[0]
  45. Timing[Map[#[[2]]&,Sort[Array[{Random[],#}&,5000]]];]
  46. {21.3167 Second, Null}
  47.  
  48. SeedRandom[0]
  49. Timing[Map[#[[2]]&,Sort[Table[{Random[],i},{i,5000}]]];]
  50. {20.8333 Second, Null}
  51.  
  52. =============================
  53.  
  54. what i really want to do is shuffle with a deck of cards which i created
  55. using 
  56.  
  57. deck = Flatten[Outer[List,{c,d,h,s}, Join[Range[2,10],{J,Q,K,A}]],1]
  58. {{c, 2}, {c, 3}, {c, 4}, {c, 5}, {c, 6}, {c, 7}, {c, 8}, {c, 9}, {c, 10},
  59. {c, J}, 
  60.  {c, Q}, {c, K}, {c, A}, {d, 2}, {d, 3}, {d, 4}, {d, 5}, {d, 6}, {d, 7},
  61. {d, 8}, 
  62.  {d, 9}, {d, 10}, {d, J}, {d, Q}, {d, K}, {d, A}, {h, 2}, {h, 3}, {h, 4},
  63. {h, 5}, 
  64.  {h, 6}, {h, 7}, {h, 8}, {h, 9}, {h, 10}, {h, J}, {h, Q}, {h, K}, {h, A},
  65. {s, 2}, 
  66.  {s, 3}, {s, 4}, {s, 5}, {s, 6}, {s, 7}, {s, 8}, {s, 9}, {s, 10}, {s, J},
  67. {s, Q},
  68.  {s, K}, {s, A}}
  69.  
  70. so, i think the fastest way to create a random deck of cards is to use:
  71.  
  72. shuffledDeck = Map[#[[2]]&,Sort[Array[{Random[],#}&,
  73. 52]]]/.Thread[Range[52]->deck]
  74.  
  75. {{s, 10}, {d, 10}, {c, 9}, {s, 3}, {c, Q}, {h, 10}, {s, 8}, 
  76.  {c, 6}, {h, 2}, {h, 3}, {d, 9}, {d, K}, {d, Q}, {c, A}, 
  77.  {h, 9}, {h, K}, {d, 8}, {d, 7}, {d, 6}, {c, J}, {h, J}, 
  78.  {s, 6}, {d, A}, {h, A}, {s, 9}, {h, 7}, {s, Q}, {c, 10}, 
  79.  {d, J}, {d, 3}, {c, 8}, {h, 5}, {s, 5}, {c, 5}, {d, 5}, 
  80.  {c, 2}, {d, 2}, {c, 7}, {c, 3}, {s, J}, {s, 7}, {h, 4}, 
  81.  {c, 4}, {d, 4}, {s, K}, {s, 4}, {h, 6}, {s, 2}, {s, A}, 
  82.  {h, 8}, {h, Q}, {c, K}}
  83.  
  84. =======================
  85.  
  86. what really started this was thinking about dealing out a bridge hand and
  87. realizing that rather than shuffling the cards and  dealing them out in
  88. the usual way, it was better to simply randomize the deck and then use
  89.  
  90. Partition[shuffledDeck, 13]
  91.  
  92. this is pretty neat too; perhaps not as neat as a christmas eve pardon
  93. [talk about playing santa], but still pleasing. too bad i don't know how
  94. to play bridge.
  95.