MathGroup Archive 2007

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

Search the Archive

Re: Quick Question


On Jun 22, 2007, at 6:50 AM, rob cronin wrote:

> Quick Question:
>
> With Mathematica 5.2 is there any way to scramble a simple list of 
> numbers:
> i.e. randomly rearange the list {1,2,3,4,5,6} without any apparent 
> order.
>
> Kind regards
>

Here is a newbie approach:

In[1]:=
lst = {1, 2, 3, 4, 5, 6}

From the Help of Union you can take UnsortedUnion

In[4]:=
UnsortedUnion[x_] :=
   Module[{f},
    f[y_] := (f[y] = Sequence[
         ]; y); f /@ x]

Then one scramble might look like :

In[7]:=
rst = UnsortedUnion[
    Table[lst[[Random[Integer,
       {1, Length[lst]}]]],
     {i, 1, Length[lst]*
       Length[lst]}]]
Out[7]=
{4, 6, 2, 3, 5, 1}

If course it is good only if the elements of the starting lst are 
distinct.

J=E1nos


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a 
corpse.
(S. Lem: His Master Voice)



  • Prev by Date: find all paths
  • Next by Date: Re: Combination List
  • Previous by thread: Re: Quick Question
  • Next by thread: Re: Quick Question