title: Shuffling Elements in a List (randomly re-arrange List Elements)
body: <p>Part of my program requires me to be able to randomly shuffle list elements. I need a function such that when i give it a list, it will pseudo-randomly re-arrange the elements in the list.<br> A change in arrangement  <strong>Must</strong> be visible at each call with the same list. <br><br>My implementation seems to work just fine but i feel that its rather long and is increasing my code base and also, i have a feeling that it ain't the best solution for doing this. So i need a much shorter implementation. Here is my implementation:</p>
  
  <pre>
  -module(shuffle).
  -export([list/1]).
  -define(RAND(X),random:uniform(X)).
  -define(TUPLE(Y,Z,E),erlang:make_tuple(Y,Z,E)).
  
  list(L)-&gt;    
      Len = length(L),
      Nums = lists:seq(1,Len),    
      tuple_to_list(?TUPLE(Len,[],shuffle(Nums,L,[]))).
  
  shuffle([],_,Buffer)-&gt; Buffer;
  shuffle(Nums,[Head|Items],Buffer)-&gt;
      {Pos,NewNums} = pick_position(Nums),    
      shuffle(NewNums,Items,[{Pos,Head}|Buffer]).
  
  pick_position([N])-&gt; {N,[]};
  pick_position(Nos)-&gt;
      T = lists:max(Nos), 
      pick(Nos,T).
  
  pick(From,Max)-&gt;
      random:seed(begin
                      (case random:seed(now()) of 
                          undefined -&gt; 
                              NN = element(3,now()),
                              {?RAND(NN),?RAND(NN),?RAND(NN)};
                          Any -&gt; Any
                      end)
                  end
                  ),
      T2 = random:uniform(Max),
      case lists:member(T2,From) of
          false -&gt; pick(From,Max);
          true -&gt; {T2,From -- [T2]}
      end.
  </pre>
  
  <p>On running it in shell: <pre>
  F:\&gt; erl
  Eshell V5.8.4  (abort with ^G)
  1&gt; c(shuffle).
  {ok,shuffle}
  2&gt; shuffle:list([a,b,c,d,e]).
  [c,b,a,e,d]
  3&gt; shuffle:list([a,b,c,d,e]).
  [e,c,b,d,a]
  4&gt; shuffle:list([a,b,c,d,e]).
  [a,b,c,e,d]
  5&gt; shuffle:list([a,b,c,d,e]).
  [b,c,a,d,e]
  6&gt; shuffle:list([a,b,c,d,e]).
  [c,e,d,b,a]
  </pre>
  I am motivated by the fact that in the <strong>STDLIB</strong> there is no such function. Somewhere in my game, i need to shuffle things up and also i need to find the best efficient solution to the problem, not just one that works.<br><br>
  Could some one help build a shorter version of the solution ? probably even more efficient ?   Thank you</p>