Re: shuffling a deck of cards
- To: mathgroup at smc.vnet.net
- Subject: [mg31797] Re: [mg31768] shuffling a deck of cards
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Sun, 2 Dec 2001 04:25:24 -0500 (EST)
- References: <200112010745.CAA01086@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Peter Dimitriou wrote: > > To Whom it may concern, > > the following is a deck of cards that needs to be shuffled, how would > I do this using the Sort Command > > Clubset=Array[clubs, 13] > > {clubs[1],clubs[2],clubs[3],clubs[4],clubs[5],clubs[6],clubs[7],clubs[8], > clubs[9],clubs[10],clubs[11],clubs[12],clubs[13]} > > Diamondset=Array[diamonds, 13] > > {diamonds[1],diamonds[2],diamonds[3],diamonds[4],diamonds[5],diamonds[6], > diamonds[7],diamonds[8],diamonds[9],diamonds[10],diamonds[11],diamonds[12], > diamonds[13]} > > Spadeset=Array[Spade, 13] > > {Spade[1],Spade[2],Spade[3],Spade[4],Spade[5],Spade[6],Spade[7],Spade[8], > Spade[9],Spade[10],Spade[11],Spade[12],Spade[13]} > > Heartset=Array[Heart, 13] > > {Heart[1],Heart[2],Heart[3],Heart[4],Heart[5],Heart[6],Heart[7],Heart[8], > Heart[9],Heart[10],Heart[11],Heart[12],Heart[13]} > > FullDeck=Sort[Join[{Heartset},{Spadeset}, {Diamondset}, {Clubset}]] > > {{clubs[1],clubs[2],clubs[3],clubs[4],clubs[5],clubs[6],clubs[7],clubs[8], > clubs[9],clubs[10],clubs[11],clubs[12],clubs[13]},{diamonds[1], > diamonds[2],diamonds[3],diamonds[4],diamonds[5],diamonds[6],diamonds[7], > diamonds[8],diamonds[9],diamonds[10],diamonds[11],diamonds[12], > diamonds[13]},{Heart[1],Heart[2],Heart[3],Heart[4],Heart[5],Heart[6], > Heart[7],Heart[8],Heart[9],Heart[10],Heart[11],Heart[12], > Heart[13]},{Spade[1],Spade[2],Spade[3],Spade[4],Spade[5],Spade[6], > Spade[7],Spade[8],Spade[9],Spade[10],Spade[11],Spade[12],Spade[13]}} > > How do I shuffle FullDeck using the Sort Command so it is all mixed > up? > > Peterangelo Your input is a bit off in that there are too many levels of List. Also why bother to Sort what you are about to shuffle? Also by convention I'll avoid beginning symbols with a capital letter. clubset = Array[clubs, 13] diamondset = Array[diamonds, 13] spadeset = Array[spade, 13] heartset = Array[heart, 13] fullDeck = Join[heartset,spadeset, diamondset, clubset] Here is a standard way to shuffle that makes explicit use of Sort. shuffle1[deck_List] := Map[#[[2]]&, Sort[Transpose[{Table[Random[],{Length[deck]}],deck}]]] An equivalent but faster variation (equivalent to something shown by Allan Hayes in a related note) uses sorting implicitly in Ordering. shuffle2[deck_List] := deck[[Ordering[Table[Random[],{Length[deck]}]]]] In[47]:= SeedRandom[1111] In[48]:= s1 = shuffle1[fullDeck]; In[49]:= SeedRandom[1111] In[50]:= s2 = shuffle2[fullDeck]; In[51]:= s1===s2 Out[51]= True Here is an asymptotically faster method. We iterate over a list, successively swapping the current element with a random element between current and end positions (inclusive). It may help if your deck has, say, a million cards. shuffle3 = Compile[{{n, _Integer}}, Module[{res=Range[n], tmp, rand}, Do[ rand = Random[Integer, {j,n}]; tmp = res[[j]]; res[[j]] = res[[rand]]; res[[rand]] = tmp, {j, 1, n}]; res ]]; I believe both methods appear in the standard add-on package Combinatorica.m. Also both have appeared in this news group from time to time. Daniel Lichtblau Wolfram Research
- References:
- shuffling a deck of cards
- From: peterangelo@mindspring.com (Peter Dimitriou)
- shuffling a deck of cards