In the following MWE I use \lhead{\stepcounter{step}\arabic{step}} and it works perfectly, incrementing by 1 each page. I also use \chead{\setrand{100}\arabic{rand}} and get the same number every page. If I call \setrand somewhere on the page, it works. If it is checking the value of rand, why doesn't it check the value of step?
\documentclass{article}
\usepackage{fancyhdr}
\makeatletter
% The following is taken from \cite{Numerical Recipes in C}.
% The resulting MAX_RAND = 134456
% Proven good statistics (relatively speaking) but designed for speed.
% Uses 8 entry shuffle.
% Output through counter rand
%
% \randinit required for initialization
% \setrand{n} outputs rand between 0 and (n-1)
% \nextrand uses same scale computed by last \setrand
\newcounter{rand}%scaled random number
\newcounter{index}% used by shuffle
\global\newcount\idum
\global\newcount\im
\global\newcount\ia
\global \newcount\ic
\im = 134456\relax
\ia = 8121\relax
\ic = 28411\relax
\newcount\temp
\def\step@rand{% computes next value for \idum (internal only)
\multiply\idum by\ia
\advance\idum by\ic
\temp = \idum% compute idum mod im
\divide\temp by\im
\multiply\temp by\im
\advance\idum by -\temp
}
\global\newcount\shuffle
\shuffle = 16807\relax
\global\newcount\storeA
\global\newcount\storeB
\global\newcount\storeC
\global\newcount\storeD
\global\newcount\storeE
\global\newcount\storeF
\global\newcount\storeG
\global\newcount\storeH
\def\shuffle@rand{% random shuffle (internal only)
\step@rand
\c@index = \idum
\step@rand
\divide\c@index by\shuffle
\advance\c@index by 1
\c@rand = \csname store\Alph{index}\endcsname
\csname store\Alph{index}\endcsname = \idum
}
\newcommand{\randinit}{% required to initialize PRNG
\idum = \day
\multiply\idum by 1440\relax
\advance\idum by \time
\step@rand%warmup
\step@rand
\step@rand
\step@rand
\storeA = \idum% fill shuffle array
\step@rand
\storeB = \idum
\step@rand
\storeC = \idum
\step@rand
\storeD = \idum
\step@rand
\storeE = \idum
\step@rand
\storeF = \idum
\step@rand
\storeG = \idum
\step@rand
\storeH = \idum
}
\global\newcount\scale
\newcommand{\setrand}[1]{% scales rand between 0 and (#1 -1)
\scale = 1\relax
\ifnum #1 > 0%check for valid #1
\scale = \im
\divide\scale by #1\relax
\fi
\shuffle@rand
\divide\c@rand by\scale
}
\newcommand{\nextrand}{% uses same scale as before
\shuffle@rand
\divide\c@rand by\scale
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
\newcounter{step}
\begin{document}
\randinit
\pagestyle{fancyplain}
\lhead{\stepcounter{step}\arabic{step}}
\chead{\setrand{100}\arabic{rand}}
\setrand{100}\arabic{rand}\par
\setrand{100}\arabic{rand}\par
\setrand{100}\arabic{rand}\par
\setrand{100}\arabic{rand}\par
\setrand{100}\arabic{rand}\par
\setrand{100}\arabic{rand}\par
\newpage
This page intentionally left blank.
\newpage
This page intentionally left blank.
\end{document}