%%
%% This is file `probsoln.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% probsoln.dtx 
%% Copyright (C) 2000 Nicola Talbot, all rights reserved.
%% If you modify this file, you must change its name first.
%% You are NOT ALLOWED to distribute this file alone. You are NOT
%% ALLOWED to take money for the distribution or use of either this
%% file or a changed version, except for a nominal charge for copying
%% etc.
%%This is the problem
%%This is the solution
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{probsoln}[2004/03/21 Problems and their Solutions Package]
\RequirePackage{ifthen}
\provideboolean{showanswers}
\newcommand{\showanswers}{\showanswerstrue}
\newcommand{\hideanswers}{\showanswersfalse}
\DeclareOption{answers}{\showanswerstrue}
\DeclareOption{noanswers}{\showanswersfalse}
\ExecuteOptions{noanswers}
\ProcessOptions
\newif\ifdefined
\def\checkdefined#1{\ifx#1\relax \definedfalse \else \definedtrue \fi}
\newif\ifselectrandom \selectrandomfalse
\newif\iffirstpass
\newif\ifselected
\newcommand{\solutionname}{Solution:}
\newcommand{\newproblem}[4][0]{%
\ifselectrandom
\iffirstpass
\global\advance\@probN by 1
\expandafter\gdef\csname @problabel\romannumeral\@probN\endcsname{#2}
\else
\checkselected{#2}
\ifselected
\expandafter\checkdefined\csname @prob@#2\endcsname
\ifdefined
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}
\else
\ifnum#1=0
\expandafter\newcommand\csname @prob@#2\endcsname[#1]{%
#3 \ifshowanswers\paragraph{\solutionname} #4\fi}
\else
\message{Problem #2 requires #1 argument(s), please specify (e.g. {5}{3}):}
\read-1to\@tmp
\expandafter\let\csname @prob@#2@arg\endcsname=\@tmp
\expandafter\newcommand\csname @prob@#2\endcsname[#1]{%
#3 \ifshowanswers\paragraph{\solutionname} #4\fi}
\fi
\fi
\fi
\fi
\else
\expandafter\checkdefined\csname @prob@#2\endcsname
\ifdefined
\PackageError{probsoln}{Label #2 already used}%
{Each problem must have a unique label identifier}
\else
\expandafter\newcommand\csname @prob@#2\endcsname[#1]{%
#3 \ifshowanswers\paragraph{\solutionname} #4\fi}
\fi
\fi
}
\newcommand{\useproblem}[1]{%
\expandafter\checkdefined\csname @prob@#1\endcsname
\ifdefined
\def\doprob{\csname @prob@#1\endcsname}
\else
\PackageError{probsoln}{Label #1 undefined}%
{Can't find problem defined with label identifier '#1'}
\def\doprob{\relax}
\fi
\doprob
}
\newcount\@probN \newcount\@probselN \newcount\@rndselctr

\newcount\r@ndcur
\r@ndcur=1\relax
\newcommand{\PSNrandseed}[1]{%
\ifnum#1=0
\typeout{Can't have 0 as random seed, changing to 1}
\r@ndcur=1\relax
\else
\r@ndcur=#1\relax
\fi
\typeout{Random Seed = \the\r@ndcur}
}

\newcount\@ps@randtmp
\newcommand{\PSNrand}{
\@ps@randtmp=\r@ndcur
\multiply\@ps@randtmp by 16807\relax
\r@ndcur=\@ps@randtmp
\divide\r@ndcur by 120001
\multiply\r@ndcur by 120001
\advance\@ps@randtmp by -\r@ndcur
\r@ndcur = \@ps@randtmp
\ifnum\r@ndcur=0\relax\r@ndcur=1\fi
}

\newcommand{\PSNrandom}[2]{%
\PSNrand
#1=\r@ndcur
\@ps@randtmp=\r@ndcur
\divide\@ps@randtmp by #2\relax
\multiply\@ps@randtmp by #2\relax
\advance#1 by -\@ps@randtmp
\advance#1 by 1\relax
}

\newcommand{\selectrandomly}[2]{%
\global\@probselN=#2
\global\@probN=0
\selectrandomtrue
\firstpasstrue
\input{#1}
\ifnum\@probselN>\@probN
\PackageError{probsoln}{Requested number too large}%
{You have asked for \the\@probselN \space problems, but there are only
\the\@probN \space problems defined in the file #1.  I will only select \the\@probN \space
problems.}
\global\@probselN=\@probN
\fi
\shuffle{@problabel}{\@probN}
\firstpassfalse
\input{#1}
\selectrandomfalse
\@rndselctr=1\relax
\whiledo{\@rndselctr < \@probselN \TE@or \@rndselctr = \@probselN}{%
\edef\@tmp{\csname @problabel\romannumeral\@rndselctr\endcsname}
\edef\@probargs{{\@tmp}\csname @prob@\@tmp @arg\endcsname}
\item \expandafter\useproblem\@probargs
\advance\@rndselctr by 1\relax
}
}
\newcount\@ckselctr
\newcommand{\checkselected}[1]{%
\selectedfalse
\@ckselctr=1\relax
\whiledo{\@ckselctr < \@probselN \TE@or \@ckselctr = \@probselN}{%
\ifthenelse{\equal{#1}{\csname @problabel\romannumeral\@ckselctr\endcsname}}%
{\global\selectedtrue \global\@ckselctr=\@probselN}{}
\advance\@ckselctr by 1\relax
}
}
\newcount\@shfctr \newcount\@shfA \newcount\@shfB
\newcommand{\shuffle}[2]{%
\@shfctr=1\relax
\whiledo{\@shfctr < 101}{%
\PSNrandom{\@shfA}{#2}\PSNrandom{\@shfB}{#2}
\ifnum\@shfA=\@shfB
\else
\edef\@@tmpA{\csname#1\romannumeral\@shfA\endcsname}
\let\@tmpA=\@@tmpA
\edef\@@tmpB{\csname#1\romannumeral\@shfB\endcsname}
\let\@tmpB=\@@tmpB
\expandafter\xdef\csname#1\romannumeral\@shfA\endcsname{\@tmpB}
\expandafter\xdef\csname#1\romannumeral\@shfB\endcsname{\@tmpA}
\fi
\advance\@shfctr by 1\relax
}
}
\endinput
%%
%% End of file `probsoln.sty'.
