[texhax] random order

Oleg Katsitadze olegkat at gmail.com
Fri Oct 13 13:04:00 CEST 2006


On Thu, Oct 12, 2006 at 01:30:12PM -0400, Tom Sgouros wrote:
> Can someone help me think of a way to get 11 items in a random order?

In plain TeX you can try the code below.  It will produce
identical results for different runs if those runs occur
within the same minute, because random.tex uses the seed
which changes only every minute -- that's what \time
returns.

------------------------------%<------------------------------
\input random

\newcount\icount
\newcount\i
\newcount\j

% Define a new item.
\def\defitem{%
  \advance\icount by1
  % Define a macro with the name `n' where n is the item's number.
  \expandafter\def \csname \number\icount \endcsname
}
% Printing of items.
\def\printitem#1{\csname \number#1\endcsname}
\let\printbetweenitems\space
% Print all defined items in random order.
\def\getitems{%
  % Unset all flags.
  \i=\icount
  \loop
    \expandafter\let \csname flag\number\i \endcsname a%
    \advance\i by-1
    \ifnum\i > 0 \repeat
  % Print random items, each item once, until every item has been
  % printed.
  \i=\icount
  \loop
    % Get random number \j.
    \setrannum{\j}{1}{\icount}%
    % Print item \j only if its flag is unset.
    \expandafter\ifx \csname flag\number\j \endcsname a%
      \expandafter\let \csname flag\number\j \endcsname b% Set the flag.
      \printitem\j
      \advance\i by-1
      \ifnum\i > 0 \printbetweenitems\fi
    \fi
    \ifnum\i > 0 \repeat
}


\defitem{1}
\defitem{2}
\defitem{3}
\defitem{4}
\defitem{5}
\defitem{6}
\defitem{7}
\defitem{8}
\defitem{9}
\defitem{10}
\defitem{11}

\getitems\par
\getitems\par
\getitems\par
\getitems\par
\getitems\par

\bye
------------------------------%<------------------------------

HTH,
Oleg


More information about the texhax mailing list