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

  1. Newsgroups: sci.math.symbolic
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!mm-mac18.mse.uiuc.edu!gaylord
  3. From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
  4. Subject: how to randomize the elements in a list?
  5. Message-ID: <BzrsII.7vu@news.cso.uiuc.edu>
  6. X-Xxdate: Thu, 24 Dec 92 09:38:24 GMT
  7. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  8. X-Useragent: Nuntius v1.1.1d13
  9. Organization: University of Illinois
  10. Date: Thu, 24 Dec 1992 15:32:41 GMT
  11. X-Xxmessage-Id: <A75F35906F019B12@mm-mac18.mse.uiuc.edu>
  12. Lines: 51
  13.  
  14. i want to randomize the elements in a list. 
  15.  
  16. i use  'a sample without replacement' function:
  17.  
  18. a program that randomly selects r numbers from the list Range[n],
  19. removing each number from the list as it is selected.
  20.  
  21. cardDealing[n_, r_] := 
  22.   NestList[Delete[#,
  23.         Random[Integer,{1,Length[#]}]]&,Range[n],r]
  24.  
  25.  
  26. SeedRandom[0]
  27.  
  28. cardDealing[10, 10]
  29. {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
  30.  {1, 2, 3, 4, 5, 6, 8, 9, 10},
  31.  {1, 2, 3, 4, 5, 8, 9, 10}, 
  32.  {1, 2, 3, 4, 8, 9, 10}, 
  33.  {1, 2, 3, 4, 9, 10}, 
  34.  {1, 3, 4, 9, 10}, 
  35.  {1, 4, 9, 10},
  36.  {1, 4, 10}, 
  37.  {1, 10}, 
  38.  {1}, {}}
  39.  
  40. it clear in this example, that the randomized list is
  41. {7,6,5,8,2,3,9,4,10,1}. the question is how to get this answer. i have a 
  42. way which is shown below 
  43.  
  44. SeedRandom[0]
  45.  
  46. Flatten[Map[Apply[Complement,#]&,Partition[NestList[Delete[#,
  47.         Random[Integer,{1,Length[#]}]]&,Range[10],10],2,1]]]
  48.  
  49. {7, 6, 5, 8, 2, 3, 9, 4, 10, 1}
  50.  
  51. basically i use Partition[cardDeal, 2, 1] to create pairs of succesive
  52. elements from the cardDealing list  and then use Map[Apply[Complement,#]&
  53. to determine what element has been removed in successive lists.
  54.  
  55. can someone come up with a one-liner which does this more directly? i
  56. should be able to just keep track of what i'm taking from the list rather
  57. than what's left in the list each time i select but i can't figure out
  58. how to do that in either in a one-liner or without an AppendTo [dreaded
  59. assignment] statement kind of thing.
  60.  
  61. anyway, any alternative code in any style for doing this would be worth
  62. looking at.
  63.  
  64. perhaps santa will leave the answer for christmas.
  65.