Re: Random Sampling Without Replacement?
- To: mathgroup at smc.vnet.net
- Subject: [mg26604] Re: [mg26586] Random Sampling Without Replacement?
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Thu, 11 Jan 2001 10:39:14 -0500 (EST)
- References: <200101090652.BAA00242@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"A. E. Siegman" wrote: > > Looking for neat compact way to extract three distinct (i.e., nonequal) > randomly selected integers k1, k2, k3 from the range 1 to N (N > 3) -- > in other words, random sampling without replacement -- ??? A general efficient way to sample without replacement may be implemented as a shuffling algorithm that iteratively swaps first element with a random element between start and end, then swaps second with a random element between second and end, and so on. In essence we shuffle as we deal (don't try this in Las Vegas). SetAttributes[deal, HoldFirst] deal[deck_, start_, end_] := Module[ {len=Length[deck], rand}, Do [ rand = Random[Integer,{j,len}]; deck[[{j,rand}]] = deck[[{rand,j}]], {j,start,end}]; Take[deck, {start,end}] ] deck = Range[52]; Let's pick four. In[178]:= deal[deck, 1, 4] Out[178]= {44, 18, 7, 9} Now we can pick some more, say seven. In[179]:= deal[deck, 5, 11] Out[179]= {36, 34, 11, 45, 8, 2, 46} Daniel Lichtblau Wolfram Research
- References:
- Random Sampling Without Replacement?
- From: "A. E. Siegman" <siegman@stanford.edu>
- Random Sampling Without Replacement?