Re: Unique List
- To: mathgroup at CHRISTENSEN.CYBERNETICS.NET
- Subject: [mg370] Re: [mg358] Unique List
- From: Dana_Scott at POP.CS.CMU.EDU
- Date: Tue, 27 Dec 94 19:29:31 EST
Robert B. Love on Mon, 26 Dec 94, in [mg358] Unique List asked:
OK, time for a little fun. I can generate a list of random
integers as Table[Random[Integer,{1,42}],{6}] and I get a list
6 elements long containing the integers from 1 to 42. But how
do I make sure no elements are repeated?
Since the numbers in question are so small, a recursive solution seems
effective enough. Here "recursive" means "keep trying Random until
you get a new integer."
In[30]:=
New[n_][s_] := If[Not[MemberQ[s, #]], Append[s,#],
New[n][s]]&[Random[Integer,{1,42}]]
In[34]:=
Nest[New[42], {}, 6]
Out[34]=
{2, 33, 5, 27, 24, 37}
Pushing our luck a little, we find:
In[33]:=
Nest[New[42], Range[15], 6]
Out[33]=
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
25, 29, 36, 20, 34, 18}
So, this seems to be a satisfactory solution. BUT, it occurred to me
that choosing a unique list out of Range[52] is a DEAL OF CARDS. Here
is a random deal of 6 cards to 4 players sorted by suit and value:
In[41]:=
(Sort /@ Partition[1+{Quotient[#,13],Mod[#,13]}& /@
Nest[New[52], {}, 24],6]) // TableForm
Out[41]//TableForm=
1 2 2 2 3 4
10 6 9 13 4 1
2 3 3 3 3 4
2 1 6 11 13 3
1 1 2 3 4 4
2 8 1 8 2 4
1 1 1 2 3 3
5 6 9 12 7 12
Has Las Vegas heard about this?