MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Unique List Summarized

Summary of the Unique List problem and a challenge
to solve the SlickDeal problem.


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?
Here are the type of solutions that were sumitted:

1) Use the built-in functions in the package:


{10, 18, 22, 24, 33, 41}

//by Richard Mercer
//by Robert Dickau


//by Benno Puetz
2) Trial and Error {Keep trying until the selection has no duplication}

While[len != 6,
               hand = Table[Random[Integer,{1,42}],{6}];
               len = Length[Union[hand]]

// by Martin McClain  
---- big problem if you try to pick 26 random numbers instead of 6 ---

Your algorithm terminates with probability 1, however, for any positive
integer n, the probability that the function you gave will not terminate
after calling Random n times, is positive.

// by Istvan Nemes  
        New[n_][s_] := If[Not[MemberQ[s, #]], Append[s,#],

// by  Dana_Scott


3) nonrecursive procedural solution to the Lotto problem:
{ Pick random number from list, remove it, and repeat.}

l = Table[k, {k, 42}];
m = l; Table[j = Random[Integer, {l, Length[m]}];
             k = m[[j]];
             m = Delete[m, j]; k,

// by  Lou Talman 
             AppendTo[S,x={CS[[ Random[Integer,{1,n-j+1}] ]]}];
             CS=Complement[CS,x] ];Flatten[S])

// by donald darling



//by Steve Skiema  --- Taken from his algorithm in the package:
 DiscreteMath`Combinatorica` for RandomKSubsets and applied to this

4)  a purely functional procedure which simulates the way numbers 
(if they were cards) could be mixed up and selected at random.

"Consider the numbers as cards from 1 to 42. Mix[numbers] by
doing a perfect shuffle followed by a random cut (RotateLeft). Nest this
process 10 times and (Take) the top 6 numbers."



{23, 17, 22, 15, 30, 41}

//by D Piele

This procedure generalizes nicely to dealing n hands of  k cards at
random from a standard deck.

Recently I posed the following problem in MiE.

E3. Slick Deal
Create a function SlickDeal[n,k] which will deal out n hands
of k cards from a standard deck of cards.

The standard deck is to be put in a list as follows


{{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}}

Solutions have been submitted that use the built-in
DiscreteMath`Combinatorica` package. 

Can you come up with one that is purely functional, like the 
one shown above, that used no packages?

My deadline for the next issue of MiE is Saturday Jan
28. If you have some new insights, I want to hear from

Good Luck!


  • Prev by Date: functional derivatives
  • Next by Date: control the messages
  • Previous by thread: functional derivatives
  • Next by thread: Unique List Summarized