home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: sci.math.symbolic
- Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!mm-mac18.mse.uiuc.edu!gaylord
- From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
- Subject: how to randomize the elements in a list?
- Message-ID: <BzrsII.7vu@news.cso.uiuc.edu>
- X-Xxdate: Thu, 24 Dec 92 09:38:24 GMT
- Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
- X-Useragent: Nuntius v1.1.1d13
- Organization: University of Illinois
- Date: Thu, 24 Dec 1992 15:32:41 GMT
- X-Xxmessage-Id: <A75F35906F019B12@mm-mac18.mse.uiuc.edu>
- Lines: 51
-
- i want to randomize the elements in a list.
-
- i use 'a sample without replacement' function:
-
- a program that randomly selects r numbers from the list Range[n],
- removing each number from the list as it is selected.
-
- cardDealing[n_, r_] :=
- NestList[Delete[#,
- Random[Integer,{1,Length[#]}]]&,Range[n],r]
-
-
- SeedRandom[0]
-
- cardDealing[10, 10]
- {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- {1, 2, 3, 4, 5, 6, 8, 9, 10},
- {1, 2, 3, 4, 5, 8, 9, 10},
- {1, 2, 3, 4, 8, 9, 10},
- {1, 2, 3, 4, 9, 10},
- {1, 3, 4, 9, 10},
- {1, 4, 9, 10},
- {1, 4, 10},
- {1, 10},
- {1}, {}}
-
- it clear in this example, that the randomized list is
- {7,6,5,8,2,3,9,4,10,1}. the question is how to get this answer. i have a
- way which is shown below
-
- SeedRandom[0]
-
- Flatten[Map[Apply[Complement,#]&,Partition[NestList[Delete[#,
- Random[Integer,{1,Length[#]}]]&,Range[10],10],2,1]]]
-
- {7, 6, 5, 8, 2, 3, 9, 4, 10, 1}
-
- basically i use Partition[cardDeal, 2, 1] to create pairs of succesive
- elements from the cardDealing list and then use Map[Apply[Complement,#]&
- to determine what element has been removed in successive lists.
-
- can someone come up with a one-liner which does this more directly? i
- should be able to just keep track of what i'm taking from the list rather
- than what's left in the list each time i select but i can't figure out
- how to do that in either in a one-liner or without an AppendTo [dreaded
- assignment] statement kind of thing.
-
- anyway, any alternative code in any style for doing this would be worth
- looking at.
-
- perhaps santa will leave the answer for christmas.
-