home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: sci.math.symbolic
- Path: sparky!uunet!wupost!uwm.edu!ux1.cso.uiuc.edu!news.cso.uiuc.edu!gaylord.slip.uiuc.edu!gaylord
- From: Richard J. Gaylord <gaylord@ux1.cso.uiuc.edu>
- Subject: Re: how to randomize the elements in a list?
- References: <BzrsII.7vu@news.cso.uiuc.edu>
- Message-ID: <Bzs4MK.AJn@news.cso.uiuc.edu>
- X-Xxdate: Thu, 24 Dec 92 13:55:13 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 19:54:19 GMT
- X-Xxmessage-Id: <A75F71C19201164B@gaylord.slip.uiuc.edu>
- Lines: 80
-
- gaylord@ux1.cso.uiuc.edu (Richard J. Gaylord)
-
- thanks to all the santas out there.
-
- Hal Varian pointed out that the standard package
- DiscreteMath/Permutations.m contains the following definition:
-
- RandomPermutation[n_Integer?Positive] :=
- Block[{t},
- t = Array[{Random[], #} &, n];
- t = Sort[t];
- Map[ #[[2]] &, t ]
- ]
-
- this is really clever [those wri guys ain't too shoddy] since it uses the
- built-in Random function which is quite fast.
-
- functionalizing this (so as not to offent my aethetic sensibilities), we
- can write
-
- randomPermutations[n_] := Map[#[[2]]&,Sort[Array[{Random[],#}&, n]]]
-
- an alternative to using Array is, of course, to use Table
-
- Map[#[[2]]&,Sort[Table[{Random[],i},{i,n}]]]
-
-
- there seems to be a small (miniscule) speed advantage to Table over Array
-
- SeedRandom[0]
- Timing[Map[#[[2]]&,Sort[Array[{Random[],#}&,5000]]];]
- {21.3167 Second, Null}
-
- SeedRandom[0]
- Timing[Map[#[[2]]&,Sort[Table[{Random[],i},{i,5000}]]];]
- {20.8333 Second, Null}
-
- =============================
-
- what i really want to do is shuffle with a deck of cards which i created
- using
-
- deck = Flatten[Outer[List,{c,d,h,s}, Join[Range[2,10],{J,Q,K,A}]],1]
- {{c, 2}, {c, 3}, {c, 4}, {c, 5}, {c, 6}, {c, 7}, {c, 8}, {c, 9}, {c, 10},
- {c, J},
- {c, Q}, {c, K}, {c, A}, {d, 2}, {d, 3}, {d, 4}, {d, 5}, {d, 6}, {d, 7},
- {d, 8},
- {d, 9}, {d, 10}, {d, J}, {d, Q}, {d, K}, {d, A}, {h, 2}, {h, 3}, {h, 4},
- {h, 5},
- {h, 6}, {h, 7}, {h, 8}, {h, 9}, {h, 10}, {h, J}, {h, Q}, {h, K}, {h, A},
- {s, 2},
- {s, 3}, {s, 4}, {s, 5}, {s, 6}, {s, 7}, {s, 8}, {s, 9}, {s, 10}, {s, J},
- {s, Q},
- {s, K}, {s, A}}
-
- so, i think the fastest way to create a random deck of cards is to use:
-
- shuffledDeck = Map[#[[2]]&,Sort[Array[{Random[],#}&,
- 52]]]/.Thread[Range[52]->deck]
-
- {{s, 10}, {d, 10}, {c, 9}, {s, 3}, {c, Q}, {h, 10}, {s, 8},
- {c, 6}, {h, 2}, {h, 3}, {d, 9}, {d, K}, {d, Q}, {c, A},
- {h, 9}, {h, K}, {d, 8}, {d, 7}, {d, 6}, {c, J}, {h, J},
- {s, 6}, {d, A}, {h, A}, {s, 9}, {h, 7}, {s, Q}, {c, 10},
- {d, J}, {d, 3}, {c, 8}, {h, 5}, {s, 5}, {c, 5}, {d, 5},
- {c, 2}, {d, 2}, {c, 7}, {c, 3}, {s, J}, {s, 7}, {h, 4},
- {c, 4}, {d, 4}, {s, K}, {s, 4}, {h, 6}, {s, 2}, {s, A},
- {h, 8}, {h, Q}, {c, K}}
-
- =======================
-
- what really started this was thinking about dealing out a bridge hand and
- realizing that rather than shuffling the cards and dealing them out in
- the usual way, it was better to simply randomize the deck and then use
-
- Partition[shuffledDeck, 13]
-
- this is pretty neat too; perhaps not as neat as a christmas eve pardon
- [talk about playing santa], but still pleasing. too bad i don't know how
- to play bridge.
-