%% LaTeX package for resolving the author ordering problem.
%%
%% Copyright (C) 2012  Toby Cubitt
%% See the files README and COPYING.
%%
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% of this license or (at your option) any later version.
%% The latest version of this license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% and version 1.2 or later is part of all distributions of LaTeX
%% version 1999/12/01 or later.

\RequirePackage{lcg}

\newcounter{AO@uthorcount}

\long\def\authorcode#1{%
  \stepcounter{AO@uthorcount}%
  \expandafter\long\expandafter\gdef%
    \csname @uthorcode\arabic{AO@uthorcount}\endcsname{#1}}

\def\makeauthorlist{%
  \@whilesw\ifnum\value{AO@uthorcount}>0\fi{%
    \chgrand[first=1,last=\value{AO@uthorcount}]\rand%
    \csname @uthorcode\arabic{rand}\endcsname%
    % drop later author codes down by one
    \@whilesw\ifnum\value{rand}<\value{AO@uthorcount}\fi{%
      \expandafter\def\expandafter\@tempa\expandafter{%
        \csname @uthorcode\arabic{rand}\endcsname}%
      \stepcounter{rand}%
      \expandafter\def\expandafter\@tempb\expandafter{%
        \csname @uthorcode\arabic{rand}\endcsname}%
      \expandafter\expandafter\expandafter\let\expandafter\@tempa\@tempb}%
    \expandafter\def\expandafter\@tempa\expandafter{%
      \csname @uthorcode\arabic{rand}\endcsname}%
    \expandafter\let\@tempa\relax%
    % decrement and repeat
    \addtocounter{AO@uthorcount}{-1}}}
